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?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaish MathewsPosted Apr 18, 2010, 12:36 AM
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);