【问题标题】:Two ComboBox with same DataSource causes Selection to be forgotten具有相同数据源的两个 ComboBox 导致选择被遗忘
【发布时间】:2018-05-08 15:55:02
【问题描述】:

我定义了BindingSource

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

然后是 ComboBoxDataSource 定义如下:

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,列表仍然存在,只是“忘记”选择了哪个。

编辑:我发现在显示第二个ComboBox 时,第一个ComboBox1 的EventHandler 以某种方式被分配给cmbContractors2Selected 而不是原来的cmbContractors1Selected

【问题讨论】:

标签: c# winforms combobox bindingsource


【解决方案1】:

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

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

这将分离货币经理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多