Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new column to datagridview

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?

like image 402
Elfoc Avatar asked Mar 06 '12 11:03

Elfoc


3 Answers

It is so easy..

 dataGridView1.Columns.Add("Column","Test");
like image 84
Elfoc Avatar answered Oct 07 '22 09:10

Elfoc


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);
like image 25
Razor Avatar answered Oct 07 '22 11:10

Razor


Make it simple, in just one line code

this.dataGridView1.Columns.Add(ColumnName, HeaderText);
like image 27
Ramgy Borja Avatar answered Oct 07 '22 10:10

Ramgy Borja