C# 向数据网格视图添加新列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9582661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:59:47  来源:igfitidea点击:

Adding new column to datagridview

c#winformsdatagridview

提问by Elfoc

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?

但它不起作用..怎么做?

采纳答案by Elfoc

It is so easy..

太容易了。。

 dataGridView1.Columns.Add("Column","Test");

回答by Razor

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

回答by Ramgy Borja

Make it simple, in just one line code

简单一点,一行代码

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