Getting blank rows after setting DataGridView.DataSource

37,516

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
37,516
Jooj
Author by

Jooj

Updated on August 28, 2020

Comments

  • Jooj
    Jooj over 3 years

    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.

  • Jooj
    Jooj over 14 years
    Do you have an short example of how to add bound columns?
  • Jooj
    Jooj over 14 years
    My code does the same like yours. Only that I set datagridview columns in the designer and they match with my tables column names.
  • Sorin Comanescu
    Sorin Comanescu over 14 years
    Then, if you're binding the grid using the designer and getting additional columns from sql is your only concern, you could simply make the undesirable columns invisible using the designer.
  • Jooj
    Jooj over 14 years
    I could make them invisible but the additional columns header texts are different from the other columns made with designer. p.s.: This is for a mulitlanguage application.
  • Sorin Comanescu
    Sorin Comanescu over 14 years
    I'm not sure I'm getting it right... As far as I understand, you have a set of columns, of which some are invisible while the other are visible. For the invisible columns the header text doesn't really matter. For the visible columns you could set the displayed header text programatically, based on the current language. Not seeing where the problem is but again, I might be missing something. Maybe a more extended code sample could help.
  • Jooj
    Jooj over 14 years
    Sorin above you can see an extended description of my problem.
  • Jooj
    Jooj over 14 years
    I belive your code would work, but I need to let the pre-defined columns in the designer because my multilanguage support is based on form definitions and automaticly reads/writes object and them properties...
  • Sorin Comanescu
    Sorin Comanescu over 14 years
    OK, I see. Then go the other way: set the grid's DataSource at design time (this should automatically add the columns to the grid) and use the designer to customize each column (visibility, header text, etc). The only code you would still have to write would be to populate the dataset.
  • Admin
    Admin over 14 years
    Have you filled your DataSet? DataAdapter = new SqlDataAdapter(); DataAdapter = CreateInventoryAdapter(); DataAdapter.TableMappings.Add("Table", "GARAGE"); DataAdapter.Fill(myDataSet); myDataView = myDataSet.Tables[0].DefaultView; dataGridView1.DataSource = myDataView