Getting blank rows after setting DataGridView.DataSource

Can someone tell me why I get blank lines 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 does not work.

The number of lines is fine, but why am I getting blank lines?

I am using Winforms.

+7
c # winforms datasource datagridview
source share
9 answers

What is contained in your DataSet?

There may be no rows in the DataTable contained in your DataSet. I would leave AutoGenerateColumns true and just manually hide those columns that you don't want to see. I have never used AutoGenerateColumns = false. However, without additional code, it will be difficult to diagnose. Try replacing these two statements (first .DataSource).

AutoGenerateColumns may not affect the corresponding binding source (DataTable from the DataSet).

DataSet needs to fill in the DataAdapter:

 // Example DataAdapter = new SqlDataAdapter(); DataAdapter = CreateInventoryAdapter(); DataAdapter.TableMappings.Add("Table", "GARAGE"); DataAdapter.Fill(myDataSet); myDataView = myDataSet.Tables[0].DefaultView; dataGridView1.DataSource = myDataView 
+3
source share

I found a problem.

I created the columns in VS datagridview designer. Not the column name, but the DataPropertyName column must match the fields in the database.

Then duplicated columns will also be hidden.

+14
source share

Try something in this direction:

 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 and Prop2 must match the column names of the table. Property 1 and Property 2 are the title text that will be displayed.

EDIT:

In the example you gave it, it looks like you are combining related columns with unrelated columns.

Do it:

1. Remove all columns added using the constructor 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; 

NTN.

+9
source share

Also, if you use AutoGenerateColumns = false, make sure you add some related columns, or you get an empty row for each record

+4
source share

I know it has been a long time since you posted this, but I had the same problem and I understood the answer, so I decided to post it so that others could benefit.

The reason your columns were empty is because you also need to set the DataPropertyName value of each of the columns in the constructor according to the database field names. Just setting the name of the project is not enough.

Hope this helps someone.

+4
source share

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;

+1
source share

Me.dgvList.AutoGenerateColumns = False

  Dim col = New DataGridViewTextBoxColumn() col.DataPropertyName = "VisitNo" col.HeaderText = "Sr. No." dgvList.Columns.Add(col) 

Working

+1
source share

Try adding this code before loading the grid.

 dataGridView.ColumnCount = 0; 
0
source share

So, I had a special implementation in which I give users the ability to save excel-like column filters.

I uploaded the results to the grid, but nothing appeared because a filter like excel in a column could not match anything.

0
source share

All Articles