Getting blank rows after setting DataGridView.DataSource
Could somebody tell me why I'm getting blank rows after running this code?
...
dataGridView.AutoGenerateColumns = false; //must开发者_Python百科 be false, else getting additional columns from SQL
dataGridView.DataSource = dataSet.Tables[0].DefaultView;
Also tried
dataGridView.Update();
but not working.
Row count is ok, but why do I get blank rows?
I'm using Winforms.
I found the problem.
I designed columns in the VS datagridview designer. Not the column name, but the column DataPropertyName must match with fields in database.
Then also duplicated columns will hide.
Try something along these lines:
grid.AutoGenerateColumns = false;
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Prop1";
col.HeaderText = "Property 1";
grid.Columns.Add(col);
col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Prop2";
col.HeaderText = "Property 2";
grid.Columns.Add(col);
grid.DataSource = dataSet.Tables[0].DefaultView;
Prop1 & Prop2 should match your table's column names. Property 1 & Property 2 is the header text to be displayed.
EDIT:
From the example you gave it looks like you're combining bound columns with unbound columns.
Do this:
1.Remove any columns added using the designer 2.Add this code:
grid.AutoGenerateColumns = false;
DataGridViewColumn colID = new DataGridViewTextBoxColumn();
colID.DataPropertyName = "customerID";
colID.HeaderText = "Ident.";
grid.Columns.Add(colID);
DataGridViewColumn colName = new DataGridViewTextBoxColumn();
colName.DataPropertyName = "customerFirstName";
colName.HeaderText = "First name";
grid.Columns.Add(colName);
grid.DataSource = dataSet.Tables[0].DefaultView;
HTH.
I know it has been a long time since you posted this, but I just had the same problem and figured out the answer so I thought I would post it so others could benefit.
The reason your columns were blank is because you also need to set the DataPropertyName of each of the columns in the designer to match your database field names. Just setting the design name isn't enough.
Hope that helps someone.
Also, if you are using AutoGenerateColumns= false, make sure you have added some bound columns, or you will get a blank row for each record
What is contained in your DataSet?
Maybe the DataTable contained in your DataSet does not have any rows. I would leave AutoGenerateColumns to true and just manually hide what columns you don't want to see. I have never used AutoGenerateColumns = false. However, without more code, it is going to be hard to diagnose. Try swapping those two statements (.DataSource first).
AutoGenerateColumns may have no effect on the corresponding binding source (DataTable from DataSet).
DataSet needs to be filled by DataAdapter:
// Example
DataAdapter = new SqlDataAdapter();
DataAdapter = CreateInventoryAdapter();
DataAdapter.TableMappings.Add("Table", "GARAGE");
DataAdapter.Fill(myDataSet);
myDataView = myDataSet.Tables[0].DefaultView;
dataGridView1.DataSource = myDataView
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;
Me.dgvList.AutoGenerateColumns = False
Dim col = New DataGridViewTextBoxColumn()
col.DataPropertyName = "VisitNo"
col.HeaderText = "Sr. No."
dgvList.Columns.Add(col)
It works
In case someone is still having the issue, my problem was that I was trying to bind to class public Fields and not properties. changing to properties I.e. {get; set;} fixed it.
So I had a custom implementation where I give the users the ability to save the excel-like filters of the columns.
I loaded results into the grid, but nothing appeared since the excel-like filter on a column couldn't match anything.
I know it's late but for my case solution was not customizing columns of datagridview. Just add a blank datagridview & bind it.
In my case no rows were visible because I generated the data by LINQ and forgot to add a call to .ToArray()
at the end. After converting the data rows to an array, it worked.
Try adding this code before loading the Grid
dataGridView.ColumnCount = 0;
精彩评论