【问题标题】:Windows Forms comboboxes are automatically synchronized; why?Windows 窗体组合框自动同步;为什么?
【发布时间】:2014-02-17 17:39:42
【问题描述】:

在 VS 2010 中,我创建了一个数据绑定表单,方法是从数据源窗口拖动到一个空表单。数据源(类型化数据集)有两列:CustomerCode 和 CustomerName。在表单的 Load 事件中我写道:

private void SalesInvoiceForm_Load(object sender, EventArgs e)
{
  //Populate customer code combobox
  var customerTableAdapter = new companyDataSetTableAdapters.CustomerTableAdapter();
  customerTableAdapter.Fill(this.companyDataSet.Customer);
  customerCodeComboBox.DataSource = this.companyDataSet.Customer;
  customerCodeComboBox.ValueMember = "Code";
  customerCodeComboBox.DisplayMember = "Code";

  //Populate customer name combobox
  //customerNameComboBox.DataSource = this.companyDataSet.Customer;
  customerNameComboBox.ValueMember = "Name";
  customerNameComboBox.DisplayMember = "Name";


  this.salesInvoiceTableAdapter.Fill(this.companyDataSet.SalesInvoice);
}

不知何故,当我从 customerCodeComboBox 中选择客户代码时,customerNameComboBox 会自动同步,即显示所选客户的姓名。 customerNameComboBox 也是如此。最初我认为我需要为每个组合框的 SelectedIndexChanged 事件处理程序编写一个代码,两个同步它们。为什么这会自动发生?因为他们的 DataSource 属性设置为相同的数据表?我认为数据表没有任何与位置相关的功能。

【问题讨论】:

    标签: winforms combobox dataset


    【解决方案1】:

    为什么会自动发生这种情况?因为他们的 DataSource 属性设置为同一个数据表?

    是的

    我认为数据表没有任何与位置相关的功能。

    它没有;但是绑定系统在后台使用CurrencyManager,它处理当前项目的概念。

    【讨论】:

    • 但是表单没有 customerBindingSource。它虽然有 salesInvoiceBindingSource。另外,我注意到 DataGridView 中的组合框也不起作用。你能解释一下吗?
    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多