I'd like to add new column to existing datagridview so:
DataColumn col = new DataColumn(( dataGridView1.ColumnCount+1).ToString());
dataGridView1.Columns.Add(col);
But it doesn't work.. how to do it?
It is so easy..
dataGridView1.Columns.Add("Column","Test");
I think that 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);
Make it simple, in just one line code
this.dataGridView1.Columns.Add(ColumnName, HeaderText);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With