שתף באמצעות


Problem with multiple combo boxes showing data from the same data source

Question

Monday, May 10, 2010 1:59 PM

Hello

I have 5 combo boxes that need to show the same list of items that come from a single dataset table. I have managed to bind the datatable to the combo boxes no problem at all.. but when I run the application and select an item from one of the combo boxes all the others change to the item I selected. After some research I believe that this problem is something to do with using the one datasource so I have tried assigning the dataset datatable to 2 different variables and used them as datasources for 2 of the comboboxes but the same still happens.

A work around to this I can see is to call the same methods and SQL select statements 5 times over to get individual datasources for each combo box but this doing that sounds crazy to me. Can anyone give any suggestions as to how I can get around this easily? Code snippet below shows 2 combox boxes and their datasources. I'm using VS2005, .Net 2

    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    ComboBox1.DataSource = AllergyListDataSet.Tables("ListOfAllergies")
    ComboBox1.DisplayMember = "Condition"
    ComboBox1.ValueMember = "AllergListID"
    ComboBox1.SelectedIndex = -1


    Condition2ComboBox.DropDownStyle = ComboBoxStyle.DropDownList
    Condition2ComboBox.DataSource = AllergyListDataSet.Tables("ListOfAllergies")
    Condition2ComboBox.DisplayMember = "Condition"
    Condition2ComboBox.ValueMember = "AllergListID"
    Condition2ComboBox.SelectedIndex = -1

All replies (4)

Monday, May 10, 2010 2:20 PM ✅Answered | 3 votes

When a group of comboboxes and other controls are bound to the same datasource they're all synchronised. I don't know how to disable that behaviour but you can work around it by creating copies of your datatable and bind each combobox to each copy:

Dim copyTable_1 As DataTable
copyTable_1 = AllergyDataSet.Tables("ListOfAllergies").Copy
ComboBox2.DataSource = copyTable_1

'etc.

Only performance counts!


Monday, May 10, 2010 2:24 PM

Thanks Sylva, that saves me having it go back to SQL 5 times. much appreciated.


Monday, May 10, 2010 2:35 PM

Then mark it as answered, please.Only performance counts!


Saturday, November 22, 2014 4:44 PM | 2 votes

You should be able to change the Binding Context for the combo box.

I found this answer on StackOverflow http://stackoverflow.com/questions/4344366/multiple-combo-boxes-with-the-same-data-source-c

Condition2ComboBox.DropDownStyle = ComboBoxStyle.DropDownList
Condition2ComboBox.BindingContext = new BindingContext();
Condition2ComboBox.DataSource = AllergyListDataSet.Tables("ListOfAllergies")
Condition2ComboBox.DisplayMember = "Condition"
Condition2ComboBox.ValueMember = "AllergListID"
Condition2ComboBox.SelectedIndex = -1