3

I have 2 DropDownList ComboBoxes on a Windows Form, both populated from the same DataSet (a staff list), but they serve different purposes (project manager/reviewer).

If I set the DataSource for both of them to the DataSet, they are both bound to the DataSet and change in tandem.

Am I missing something, or will I have to read the rows and columns of the data set into the Items collection programmatically instead of using the DataSet directly?
Or replicate the DataSet?

On another form, I have the same problem several times.

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Blind Fury
  • 469
  • 3
  • 15

2 Answers2

7

Found this solution at bytes.com

combo1.DataSource = payDS.Tables[0];
combo1.BindingContext = new BindingContext();
combo1.DisplayMember = "staff_name";
combo1.ValueMember = "staff_id";

combo2.DataSource = payDS.Tables[0];
combo2.BindingContext = new BindingContext();
combo2.DisplayMember = "staff_name";
combo2.ValueMember = "staff_id";

Does the trick for me.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Blind Fury
  • 469
  • 3
  • 15
  • This may work but again, DataTable is pretty heavy structure. You may use light reader to load a list of unanimous Name/Value objects. If you would have DataSet with relationship between tables and one of these tables would be a look-up table, you could achieve look-up value change by binding comboBox to this table. sometimes you want to carry more info, than you may create more complex type for comboBox item. Then you can use `(YourComboboxItemType)combobox.SelectedItem` to retrieve information. – T.S. Nov 18 '13 at 16:23
1

This is correct behavior. Basically, you need to clone data, so that actual data source will be different for both. The only compelling reason to have combo sourced directly of datatable is when you actually want to change data in your datatable.

What you can do is to use Linq to DataTable and select what you need into unanimous type with name and value. Then use combo.Datasource, combo.ValueMember and combo.DisplayMember to load your dropdown.

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Correct is subjective. If you have two inputs that reference a common list but must allow selecting different values, it is most certainly incorrect. – Suncat2000 Aug 21 '20 at 19:45
  • @Suncat2000 - please read again. I said this: "you need to clone data". Which implies that there will not be a unique list. But rather each control will have a clone of the original – T.S. Aug 21 '20 at 19:49