Ok more advanced sample:
I made several columns in the constructor:
column names: customerID and customerFirstName
column headerText: Ident. and First name 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 columns of the sql table are the same as the column names in the dataGridView.
The result that I get when dataGridView.AutoGenerateColumns = false; represents two columns of dataGridView with headerText Ident. | First name Ident. | First name Ident. | First name .
When I set dataGridView.AutoGenerateColumns = true; , then I get the columns of the dataGridView as follows: Ident. | First name | customerID | customerFirstName Ident. | First name | customerID | customerFirstName Ident. | First name | customerID | customerFirstName .
all lines below Ident and First name are empty , and all the others below customerID and customerFirstName are ok .
Now I want the rows below customerID and customerFirstName be under Ident and First name , and the 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 does DataGridView not do this for me with this code:
dataGridView.DataSource = dataSet.Tables[0].DefaultView;
Jooj
source share