Ok a more extended sample:
I made in the designer some columns:
column names: customerID and customerFirstName
column headerText: Ident. and First name
then I get some data from sql table...
sql = "select customerID, customerFirstName From customer;";
dataGridView.AutoGenerateColumns = false;
dataGridView.DataSource = dataSet.Tables[0].DefaultView;
and the sql table columns are the same like column names in dataGridView.
The result I get when dataGridView.AutoGenerateColumns = false; is a two column dataGridView with headerText Ident. | First name.
When I set dataGridView.AutoGenerateColumns = true; then I get dataGridView columns like this:
Ident. | First name | customerID | customerFirstName.
all rows below Ident and First name are empty and all other below customerID and customerFirstName are ok.
Now I want that the rows below customerID and customerFirstName would be under Ident and First name and columns customerID and customerFirstName should be hidden.
I wrote this code and it works:
DataTable dTable = dataGridView.GetTable().Tables[0];
foreach (DataRow dataRow in dataGridView.GetTable().Tables[0].Rows)
{
int n = dataGridView.Rows.Add();
foreach (DataColumn dataColumn in dTable.Columns)
{
dataGridView.Rows[n].Cells[dataColumn.ColumnName].Value = dataRow[dataColumn.ColumnName].ToString();
}
}
But why DataGridView dosen't do this for me with this code:
dataGridView.DataSource = dataSet.Tables[0].DefaultView;