具有相同数据源的两个 ComboBox 导致 Selection 被遗忘
Two ComboBox with same DataSource causes Selection to be forgotten
我已经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);
这很好用。
我使用相同的数据源在另一个 Form
上定义了另一个 ComboBox
:
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
,列表仍然存在,只是 'forgot' 选择了哪个。
编辑:我发现当显示第二个 ComboBox
时,第一个 ComboBox1
的 EventHandler 以某种方式分配给 cmbContractors2Selected
而不是原来的 cmbContractors1Selected
尝试给它自己的绑定对象:
this.cmbContractorName2.DataSource = new BindingSource(projectView.bsContractors, null);
这将分离货币经理。
我已经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);
这很好用。
我使用相同的数据源在另一个 Form
上定义了另一个 ComboBox
:
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
,列表仍然存在,只是 'forgot' 选择了哪个。
编辑:我发现当显示第二个 ComboBox
时,第一个 ComboBox1
的 EventHandler 以某种方式分配给 cmbContractors2Selected
而不是原来的 cmbContractors1Selected
尝试给它自己的绑定对象:
this.cmbContractorName2.DataSource = new BindingSource(projectView.bsContractors, null);
这将分离货币经理。