Could somebody tell me why I'm getting blank rows after running this code?
...
dataGridView.AutoGenerateColumns = false; //must 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.
Solution 1 Either you strip off the data binding and do the boilerplate code yourself. Or you add a new record to the datasource and refresh the gridview => it will update itself with the new row.
Step 1: Make a database with a table in SQL Server. Step 2: Create a Windows Application and add DataGridView on the Form. Now add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.
You can find out if it is empty by checking the number of rows in the DataGridView. If myDataGridView. Rows. Count == 0 then your DataGridView is empty.
You can add a single row of data by calling the AddData or AddLine commands of a Data Grid.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With