how to add column dynamically in datagridview using c#
4 Answers
For example:
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Hi there";
int colIndex = grid.Columns.Add(col);
1 Comment
Totoro53
I get 'DataGridViewColumn' is a type and cannot be used as an expression.
If it's like any of the other .Net grid controls:
YourDataGridView.Columns.Add(New DataGridViewColumn());
2 Comments
Tim Goodman
Columns.Add can throw an exception if the CellType property of your DataGridViewColumn is null. I believe it's better to set the DataGridViewColumn's CellTemplate before adding it (CellType is the runtime type of CellTemplate), or to use a class that inherits from DataGridViewColumn for which CellTemplate is already set appropriately.
ChadT
@Tim Goodman - I'd agree, my example is just to show basically how it can be done. An actual implementation would be a bit more involved.
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