views:

152

answers:

3

how to add column dynamically in datagridview using c#

+2  A: 

If it's like any of the other .Net grid controls:

YourDataGridView.Columns.Add(New DataGridViewColumn());
DaRKoN_
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.
Tim Goodman
@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.
DaRKoN_
+2  A: 

For example:

        DataGridViewColumn col = new DataGridViewTextBoxColumn();
        col.HeaderText = "Hi there";
        int colIndex = grid.Columns.Add(col);
Marc Gravell
+2  A: 

These links shall help you out doing more than just adding columns:

101 Ways to Manipulate the DataGridView Control (page 1)
101 Ways to Manipulate the DataGridView Control (page 2)

Will Marcouiller