views:

202

answers:

2

I have a main class which creates and populates a DataSet and an instance of this class is then passed to sub controls of my application by reference. I want to use this dataset to databind to components, in this case, a listbox. This is for a windows forms application.

Heres what I'm trying:

channelTypesLB.DataBindings.Add("Text", synData.ds, "ChannelTypes.channelType");

Note, I've also tried this: (not sure if theres a difference)

channelTypesLB.DataBindings.Add("Text", synData.ds.Tables["ChannelTypes"], "channelType");

Theres no errors and I do not see the data in the listbox... when I output synData.ds.Tables["ChannelTypes"].Rows.Count it tells me that there is in fact data in this datatable.

Am I missing something? I also trued channelTypesLB.Refresh(); after setting the databinding.

This may also be helpful... this is the code in my main class where the dataset is created, not sure if its maybe a scope issue, I'd imagine I would have received an error:

private DataSet _ds = new DataSet();
public DataSet ds { get { return _ds; } }
+3  A: 

Try setting the Listbox's DataSource rather than binding to the Text property:

channelTypesLB.DataSource = synData.ds;
channelTypesLB.DisplayMember = "ChannelTypes.channelType";

I'm not a DataBinding expert by any means, but I believe that by databinding the way you're currently doing it the control is binding to a PropertyManager which is used to bind one value of an object to one value of another. By setting the DataSource it should bind to a CurrencyManager which is used to bind to a Collection.

Here's a quick overview of WinForms databinding

STW
Hrmm that worked... used DisplayMember instead of DataMember... I would prefer the 1 liner of using DataBindings though
Chris Klepeis
whoops, that was a typo--no such thing as a DataMember. You can also build a `BindingSource`, which itself has a DataSource and DisplayMember (although they are accesible via constructor I believe) and set ListBox.DataSource = BindingSource
STW
Thanks for pointing me in the right direction
Chris Klepeis
+1  A: 

I'm assuming winforms here since there is no ASP.NET tag...

channelTypesLB.DataSource = synData.ds.Tables["ChannelTypes"].DefaultView;
channelTypesLB.DisplayMember = "channelType";

Just in case it is ASP.NET though try:

channelTypesLB.DataSource = synData.ds.Tables["ChannelTypes"].DefaultView;
channelTypesLB.DataTextField = "channelType";
channelTypesLB.DataValueField = "channelTypeId"; // I'm assuming this field exists, replace with your id field

channelTypesLB.DataBind();
free-dom
Yes, winforms. Any idea why databindings wouldnt work?
Chris Klepeis