1

I have BindingSource defined:

public System.Windows.Forms.BindingSource bsContractors;
this.bsContractors.DataSource = typeof(Contractor);

and then a ComboBox with a DataSource defined like so:

private System.Windows.Forms.ComboBox cmbConstructionContractors1;
this.cmbConstructionContractors1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bsProject, "Id", true));
this.cmbContractors1.DataSource = this.bsContractors;
this.cmbContractors1.DisplayMember = "Name";
this.cmbContractors1.ValueMember = "Id";
this.cmbContractors1.SelectedIndexChanged += new System.EventHandler(this.cmbContractor1Selected);

This works fine. I have another ComboBox defined on another Form using the same DataSource:

this.cmbContractorName2.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", myView.bsProject, "Id", true));
this.cmbContractorName2.DataSource = projectView.bsContractors;
this.cmbContractorName2.ValueMember = "Id";
this.cmbContractorName2.DisplayMember = "Name";
this.cmbContractorName2.SelectedIndexChanged += new System.EventHandler(this.cmbContractor2Selected);

When this 2nd ComboBox is displayed, the first ComboBox, which has something selected, gets reset to the first entry, which is blank. If I pull down on the first ComboBox, the list is still there, it just 'forgot' which one was selected.

Edit: I've discovered that when displaying the 2nd ComboBox, the EventHandler of the 1st ComboBox1 somehow gets assigned to cmbContractors2Selected instead of the original cmbContractors1Selected

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • 1
    to make them independent each combobox needs it __own__ bindingsource! – TaW May 08 '18 at 16:40
  • You can read about the reason and possible solutions here: [Bind multiple ComboBox to a single List - Issue: When I choose an item, all combo boxes change](https://stackoverflow.com/a/35865838/3110834) – Reza Aghaei May 08 '18 at 19:52

1 Answers1

1

Try giving it its own binding object:

this.cmbContractorName2.DataSource = new BindingSource(projectView.bsContractors, null);

This will separate the currency managers.

LarsTech
  • 80,625
  • 14
  • 153
  • 225