Reputation: 10003
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; } }
Upvotes: 0
Views: 1183
Reputation: 5362
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();
Upvotes: 1
Reputation: 46414
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
Upvotes: 3