具有相同DataSource的两个ComboBox会导致选择被遗忘

时间:2018-05-08 15:55:03

标签: c# winforms combobox bindingsource

我已定义BindingSource

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

然后ComboBox DataSource定义如下:

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);

这很好用。 我使用相同的DataSource在另一个ComboBox上定义了另一个Form

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);

当显示第二个ComboBox时,选择了某个内容的第一个ComboBox将重置为第一个条目,该条目为空。 如果我按下第一个ComboBox,列表仍然存在,只是'忘记'选择了哪一个。

修改:我发现在显示第二个ComboBox时,第一个ComboBox1的EventHandler会被分配到cmbContractors2Selected而不是原来的cmbContractors1Selected

1 个答案:

答案 0 :(得分:1)

尝试给它自己的绑定对象:

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

这会将货币经理分开。