如何使用c#在datagridview中动态添加列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2377400/
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
how to add column dynamically in datagridview using c#
提问by ratty
how to add column dynamically in datagridview using c#
如何使用c#在datagridview中动态添加列
回答by ChadT
If it's like any of the other .Net grid controls:
如果它像任何其他 .Net 网格控件:
YourDataGridView.Columns.Add(New DataGridViewColumn());
回答by Marc Gravell
For example:
例如:
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Hi there";
int colIndex = grid.Columns.Add(col);
回答by NIco
I you mean to add all value on particular column then maybe it could help you.
我的意思是在特定列上添加所有值,然后它可能会对您有所帮助。
Try this tips:
试试这个提示:
for (int i=0; i< datagriview1.coulums.count -1; i++)
{
int total + = total + datagridview1.columns[i]
.cell[index for columns you want to add].formatedvalue.toString();
}
textbox1.text=total;
Try that code. it can help you.
试试那个代码。它可以帮助你。
Thanks, Nico
谢谢,尼科
回答by Md.Rezwan
I am also tested this code you mean to add all value on particular column then maybe it could help you. Try this tips:
我还测试了这段代码,您的意思是在特定列上添加所有值,然后它可能会对您有所帮助。试试这个提示:
gridView1.Columns.Clear();
gridView1.Columns.Add(new GridColumn() {Caption = @"Caption", FieldName = "FieldName", Name = "FieldName", Visible = true});