简体   繁体   中英

how to add column dynamically in datagridview using c#

如何使用C#在Datagridview中动态添加列

For example:

        DataGridViewColumn col = new DataGridViewTextBoxColumn();
        col.HeaderText = "Hi there";
        int colIndex = grid.Columns.Add(col);

如果它像其他任何.Net网格控件一样:

YourDataGridView.Columns.Add(New DataGridViewColumn());

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

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

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