简体   繁体   中英

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?

这很容易..

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

只需一行代码即可轻松实现

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM