8

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?

3 Answers 3

23

It is so easy..

 dataGridView1.Columns.Add("Column","Test");
Sign up to request clarification or add additional context in comments.

Comments

9

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);

1 Comment

DataGridViewCell is abstract, now. Use DataGridViewCell cell = new DataGridViewTextBoxCell(); for example.
1

Make it simple, in just one line code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.