Hi,
I use C# (winform, .net 2.0) to create a DataGridView programmatically to which I connect a BindingSource. Afterwards I want to change the column order (display index property) and the name of the columns (header text property). But when I try to access the column collection of the DataGridView, the debugger always tells me the column count is 0 even though the DataSource I correctly connected (I can view the DataTable assigned to the BindingSource in the debugger) and hence I cannot access the columns, e.g. datagridview.Columns[oldName].HeaderText. I always get a NullReferenceException?
Am I missing something? Any hints why the DataGridColumnCollection is always 0? Setting AutoGenerateColumns = true before assigning the BindingSource didn't help as well as calling Refresh or Update...
Thanks in advance!
Best regards,
Andi
DataGridView CreateDataGridView(DataTable datatable)
{
DataGridView standard = new DataGridView();
standard.AutoGenerateColumns = true;
BindingSource bs = new BindingSource();
bs.DataSource = datatable;
standard.DataSource = bs;
return standard;
}