7

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jooj
  • 687
  • 4
  • 15
  • 32

12 Answers12

20

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Jooj
  • 687
  • 4
  • 15
  • 32
9

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.

Sorin Comanescu
  • 4,829
  • 3
  • 29
  • 38
  • My code does the same like yours. Only that I set datagridview columns in the designer and they match with my tables column names. – Jooj Nov 17 '09 at 15:59
  • 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. – Sorin Comanescu Nov 17 '09 at 16:13
  • 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. – Jooj Nov 17 '09 at 16:26
  • 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. – Sorin Comanescu Nov 17 '09 at 16:40
  • Sorin above you can see an extended description of my problem. – Jooj Nov 17 '09 at 17:02
  • 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... – Jooj Nov 17 '09 at 17:42
  • 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. – Sorin Comanescu Nov 17 '09 at 20:20
5

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.

Ryan Roper
  • 333
  • 1
  • 3
  • 7
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

tardomatic
  • 2,396
  • 3
  • 21
  • 29
3

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
  • Guys thanks for your help, but I don't want to spend any more time to this. My extended code also works fine, so don't want to change it any more. Best regards – Jooj Nov 18 '09 at 12:30
2

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.

Y.S
  • 1,860
  • 3
  • 17
  • 30
1

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;

Jooj
  • 687
  • 4
  • 15
  • 32
  • 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 –  Nov 17 '09 at 20:33
1

Me.dgvList.AutoGenerateColumns = False

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

It works

Murtuza
  • 11
  • 1
0

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.

Josué Zatarain
  • 791
  • 6
  • 21
0

I know it's late but for my case solution was not customizing columns of datagridview. Just add a blank datagridview & bind it.

SAQ
  • 55
  • 10
0

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.

Andi
  • 3,234
  • 4
  • 32
  • 37
-1

Try adding this code before loading the Grid

dataGridView.ColumnCount = 0;
AArias
  • 2,558
  • 3
  • 26
  • 36