Adding new column to datagridview

I want to add a new column to an existing datagridview so that:

DataColumn col = new DataColumn(( dataGridView1.ColumnCount+1).ToString());
dataGridView1.Columns.Add(col);

      

But it won't work .. how to do it?

+3


source to share


3 answers


It is so simple.



 dataGridView1.Columns.Add("Column","Test");

      

+14


source


I think you need to specify what type of cell the column will contain.

For example:



DataGridViewColumn  newCol = new DataGridViewColumn(); // add a column to the grid
DataGridViewCell cell = new DataGridViewCell(); //Specify which type of cell in this column
newCol.CellTemplate = cell;

newCol.HeaderText = "test2";
newCol.Name = "test2";
newCol.Visible = true;
newCol.Width = 40;

gridColors.Columns.Add(newCol);

      

+5


source


Keep it simple, just one line

this.dataGridView1.Columns.Add(ColumnName, HeaderText);

      

0


source







All Articles