ComboBoxes that share the same DataSource, change the selection together

4

I'm doing a production order project and I have 7 combobox picking up the values from the same table (which are those of raw materials). I am using this code in load of the form to load the combobox:

cmbMP1.DataSource = bllprodutos.DtMateriaPrima;
cmbMP1.DisplayMember = "Descricao";
cmbMP1.ValueMember = "ID";
cmbMP1.Refresh();

cmbMP2.DataSource = bllprodutos.DtMateriaPrima;
cmbMP2.DisplayMember = "Descricao";
cmbMP2.ValueMember = "ID";
cmbMP2.Refresh();
//.......

So far so good, when the form loads the list of ComboBox mounts, but here comes the headache. When I select any value from any of them all the other ComboBox get the same value, type I can not select different products between them.

    
asked by anonymous 06.06.2016 / 18:17

1 answer

2

It turns out that ComboBox ends up creating (even without the programmer's actual intention) a BindingContext for both ComboBoxes , since you are using the same object as DataSource .

To get rid of this behavior, you need to declare a new BindingContext to all ComboBoxes that will use this object.

// resto do código
cmbMP2.BindingContext = new BindingContext();
cmbMP2.DataSource = bllprodutos.DtMateriaPrima;
//resto do código
    
06.06.2016 / 18:34