Hello:
I have a DataGridView that does not have any columns defined. I would like to add the columns and headers at run time.
Can someone tell me the correct format of the line?
How Do I: Populate the headers in a DataGridView?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Jaish MathewsRank #225 · 11K reputationPosted Apr 17, 2010, 8:36 PM EDT
Below lines will add 2 columns with behaviours defined in CellTemplate. Error normally committing is that CellTemplate not assigning to the specified Column.
DataGridViewColumn col;
DataGridViewCell cell;
//Creation of 1st column
col = new DataGridViewColumn();
col.HeaderText = "Column1";
cell = new DataGridViewTextBoxCell();
cell.Style.BackColor = Color.Blue;
col.CellTemplate = cell;
dataGridView1.Columns.Add(col);
//Creation of 2nd Column
col = new DataGridViewColumn();
col.HeaderText = "Column2";
cell = new DataGridViewTextBoxCell();
cell.Style.BackColor = Color.Red;
col.CellTemplate = cell;
dataGridView1.Columns.Add(col);