views:

1332

answers:

3

I have a problem with binding data using BindingSource, typed dataset and DataGridView. My problem is: BindingSource, therefore grid, is empty after binding data (bindingSource.Count is 0). I couldn't figure out what I'm doing wrong and I'd be very happy if anyone could help me with this. My application structure is like this: I have two assemblies, one is Winforms UI and other is database class library.

UI

  • DataGridView, datasource as BindingSource
  • BindingSource, datasource = DBAssembly.typedDataset, datamember = DBAssembly.typedDataset.myTable

Database assembly

  • Sql Server CE database
  • Typed Dataset
  • DB class for database operations

UI Code

OnLoad

this.db = new DB(); 
db.BindData();

DB Code

constructor

create typedDataSet object 
create typedDataSetTableAdapters.MyTableTableAdapter object 
create typedDataSetTableAdapters.TableAdapterManager object

BindData()

this.myTableTableAdapter.Fill(this.typedDataSet.MyTable)

I'd appreciate any help with this.

+1  A: 

Well, the first thing to ask would be: what is this.typedDataSet.MyTable.Rows.Count? i.e. did the adapter do anything? If not, it has nothing to do with the binding.

Assuming it is non-empty, then what exact code are you using to set up DataSource and DataMember? I expect it should be:

bindingSource.DataSource = this.typedDataSet;
bindingSource.DataMember = "MyTable";
dataGridView.DataSource = bindingSource;

Alternatively, you could set the bindingSource's DataSource to this.typedDataSet.MyTable and leave the table blank.

Basically, I expect that you've told it the type of data to anticipate, but so far you haven't actually given it the data-set/data-table you want to use.

Marc Gravell
A: 

Thanks for the answer Marc.

this.db.dbDataSet.MyTable in UI is not empty, it has records.

This is the code I use for sources:

this.bindingSource.DataMember = "MyTable";
this.bindingSource.DataSource = typeof(DBAssembly.dbDataSet);
this.grid.DataSource = this.bindingSource;

Mind you, this the designer generated code. I chose the class with DataSource dropdown in bindingSource properties grid.

I tried to remove typeof and use this.bindingSource.DataSource = DBAssembly.dbDataSet; manually but that gave me this error:

'DBAssembly.dbDataSet' is a 'type', which is not valid in the given context

Armagan
A: 

Basically, I expect that you've told it the type of data to anticipate, but so far you haven't actually given it the data-set/data-table you want to use.

Marc you were right about this one. I hade to create a public DataSet property in my DB class and use it as DataSource of BindingSource to make this work. Thanks for the help.

Armagan