[英]Adding new column to datagridview
I'd like to add new column to existing datagridview so: 我想在现有的datagridview中添加新列,以便:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.