bernie2436
bernie2436

Reputation: 23901

why does my datagrid view show only blank rows when I set it's datasource to my dataset

I'm brand new to binding datagridviews to datasets. I have a created a dataset and (I think) filled it with a table from a sql server DB. But when I try to fill the datagridview, it only shows a blank, 7-column table with column headings like Column1, Column2 etc. I'm sure I'm missing something silly, but don't where to begin to look. Anybody see what's wrong?

Edit It seems like my dataset is not getting filled. Dataset1.tables(0) has no rows in it. From the GUI it looks like there is something called AverageWeeklyLabsTableAdapter (see photo below). I guess I assumed datasets worked like LINQ to SQL where the object is just filled with the data automatically, but maybe you need to call code to fill the dataset?

The on load event...

    Dim myBindingSource As BindingSource = New BindingSource
    myBindingSource.DataSource = DataSet1.Tables(0) //has no rows
    DataGridView1.DataSource = myBindingSource
    DataGridView1.ReadOnly = True

The dataset

enter image description here

Upvotes: 1

Views: 3002

Answers (1)

Taryn
Taryn

Reputation: 247620

When I load data to a datagridview, I use a BindingSource but from your limited code it doesn't look like you are doing that.

So you would Fill your DataSet with the table data then load your BindingSource with your data, similar to this (code shortened for brevity):

dataAdapter.Fill(table); 
bindingSource1.DataSource = table;
datagridview1.DataSource = bindingSource1;

Based on your EDIT it doesn't appear that you are actually populating your data. You have to use the Fill from the table adapter to get the data from the database. Also, .net will expect a specific object-type for the datatable (based on the dataset) Kind of like this:

var adapter = new AverageWeeklyLabsTableAdapter();
var dataTable = new myForm.DataSet1.AverageWeeklyLabsDataTable
adapter.Fill(dataTable);
bindingSource1.DataSource = dataTable;
datagridview1.DataSource = bindingSource1;

Here is another question on SO about tableadapters:

What do C# Table Adapters actually return?

Upvotes: 1

Related Questions