【问题标题】:Selected index combobox winforms选定的索引组合框 winforms
【发布时间】:2016-05-10 17:36:03
【问题描述】:

好的,非常愚蠢的问题,但我还没有在互联网上真正找到答案。 我在一个表单上有多个comboboxes。每个comboboxbinding 在form_load 上。

当表单加载时,表单上的第一个项目被选中。这很明显,但我不想要这个。所以我在 form_load 中使用了以下代码:

private void InvoiceView_Load(object sender, EventArgs e)
{
    // Bind list of customers to combobox
    CustomerComboBox.DataSource = invoicePresenter.getCustomers();
    CustomerComboBox.DisplayMember = "CustomerName";
    CustomerComboBox.ValueMember = "CustomerId";

    // Bind list of products to combobox
    productCombobox.DataSource = invoicePresenter.getProducts();
    productCombobox.DisplayMember = "ProductName";
    productCombobox.ValueMember = "ProductId";

    // Bind list of vat codes to combobox
    vatComboBox.DataSource = invoicePresenter.getTaxCodes();
    vatComboBox.DisplayMember = "taxCodeShortDescr";
    vatComboBox.ValueMember = "taxCodeId";


   // Set comboboxes empty
    CustomerComboBox.SelectedItem = null;
    productCombobox.SelectedItem = null;
    vatComboBox.SelectedItem = null;    
}

这行得通。但是文本框仍然给我第一项的数据?我的猜测是因为它在 selectedIndexChanged 中。但我不知道还能用什么。

如果我输入 combobox.selectedIndex = -1; 我会遇到同样的问题。

代码:

private void CustomerComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    // Bind the selected customer itemvalues to the texboxes
    txtCustomerName.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerName.ToString();
    txtAddress.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerAddress.ToString();
    txtPostalCode.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerPostalCode.ToString();
    txtCity.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCity.ToString();
    txtCountry.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCountry.ToString();
    txtVatNumber.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCountryCode.ToString() + ((tbl_customer)CustomerComboBox.SelectedItem).CustomerVat.ToString();
}

有人会想,因为我在文本框绑定中使用了 selecteditem,所以它也会为 null。但事实并非如此。

【问题讨论】:

  • 如果设置了数据绑定并加载数据,则绑定上下文的当前位置在第一条记录(如果可用)上。所以行为是正常的和预期的。如果这个事实对您来说还不够,请详细描述需求,并让我们清楚地了解表单和数据绑定的结构。
  • @RezaAghaei:我已将代码更新为实际必需的。我同意采用第一个可用记录的数据绑定。但在此之后,我说 selectedItem 必须为空。当我进入调试模式时,我可以看到所选项目正确为空。虽然它仍然在文本框中显示值。那是我无法绕过的地方。要求是当我的表单加载时,它必须是空的。
  • 您应该更多地描述表单结构、目标和控件的数据绑定。例如,我们应该知道文本框和组合框绑定到哪个数据源。我们还应该知道您的组合框的哪个属性参与了数据绑定。
  • 请发帖mcve
  • 也许您忘记将方法附加到SelectedIndexChanged 事件并且它不会触发。无论如何,在这里创建一个 mcve 真的很容易。用IdName 创建一个Customer 类,然后用两个新的Customer 填充List<Customer>,然后设置组合DataSourceDisplayMemberValueMember,然后将事件处理程序附加到SelectedIndexChange 并设置 TextBox 值。这只是几行代码。这将是一个独立的测试,可以帮助您找到问题或确认重现代码中存在问题。然后我们可以提供更多帮助:)

标签: c# winforms data-binding combobox


【解决方案1】:

有趣的是,您似乎遇到了 WF 数据绑定的怪癖之一。问题是由于维护每个列表数据源的CurrencyManager 类在列表计数不为零时不允许将Position 属性设置为-1(因此Currentnull)。由于ComboBox 正在将SelectedIndexCurrencyManager.Position 同步,这有效地防止了未选择的项目。

作为一种解决方法,如果列表部分的数据绑定模式对您来说不是必需的,请替换该行

CustomerComboBox.DataSource = invoicePresenter.getCustomers();

foreach (var customer in invoicePresenter.getCustomers())
    CustomerComboBox.Items.Add(customer);

对需要此类行为的其他组合框执行相同操作。

【讨论】:

  • 非常感谢。显然在 stackoverflow 和谷歌上有很多关于这个的错误信息。这很好用
【解决方案2】:

您可以删除组合框的SelectedIndex_Changed 事件的处理程序,绑定数据,然后重新添加处理程序。像这样:

 private void InvoiceView_Load(object sender, EventArgs e)
 {
     this.CustomerComboBox.SelectedIndexChanged -= new EventHandler(CustomerComboBox_SelectedIndexChanged);
     this.productCombobox.SelectedIndexChanged -= new EventHandler(productCombobox_SelectedIndexChanged);
     this.vatComboBox.SelectedIndexChanged -= new EventHandler(vatComboBox_SelectedIndexChanged);

     // Bind list of customers to combobox
     CustomerComboBox.DataSource = invoicePresenter.getCustomers();
     CustomerComboBox.DisplayMember = "CustomerName";
     CustomerComboBox.ValueMember = "CustomerId";

     // Bind list of products to combobox
     productCombobox.DataSource = invoicePresenter.getProducts();
     productCombobox.DisplayMember = "ProductName";
     productCombobox.ValueMember = "ProductId";

     // Bind list of vat codes to combobox
     vatComboBox.DataSource = invoicePresenter.getTaxCodes();
     vatComboBox.DisplayMember = "taxCodeShortDescr";
     vatComboBox.ValueMember = "taxCodeId";


     this.CustomerComboBox.SelectedIndexChanged += new EventHandler(CustomerComboBox_SelectedIndexChanged);
     this.productCombobox.SelectedIndexChanged += new EventHandler(productCombobox_SelectedIndexChanged);
     this.vatComboBox.SelectedIndexChanged += new EventHandler(vatComboBox_SelectedIndexChanged);

 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2012-12-16
    相关资源
    最近更新 更多