Comboboxes that share the same Datasource, swap the selection together

Asked

Viewed 53 times

4

I am doing a production order project and I have 7 combobox picking the values from the same table (which are the raw materials). I’m using this code on load 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 assembles the list of Combobox, but here comes the headache. When I select any value from any of them all the other Combobox have the same value, like I can’t select different products between them.

1 answer

2


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

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

// resto do código
cmbMP2.BindingContext = new BindingContext();
cmbMP2.DataSource = bllprodutos.DtMateriaPrima;
//resto do código
  • opaaaa Jbueno... I tested here and gave it right... vlw msm guy thank you/

  • Always at your disposal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.