【问题标题】:Fill a textbox and combobox based on a combobox - Winforms基于组合框填充文本框和组合框 - Winforms
【发布时间】:2012-02-15 17:50:01
【问题描述】:

我的代码有问题。它正确显示了组合框的内容,但只要数据为空,它就不会返回为空。

我的第一个组合框是客户名称,在我单击连接到数据库的组合框中的客户名称后,它应该将他的性别放在组合框中。

*注意这是一个编辑表单,所以我要做的是更新

如果该字段有输入是正确的,但是如果不是,则如果该字段为空,则组合框中存储的值不会更改

例如:

选择客户 1 *在组合框中显示他的性别(例如女性) 选择客户 2 * 性别是女性 WTF? (假设用户忘记输入他的性别)

***所以我的问题是,如果该字段为空,我的组合框不会为空,而是保留我使用的前一个值的值。

这是我的代码。 **combobox1 是客户姓名列表,combobox2 是性别列表

private void BindControls() // function to bind the database to the comboBox
        {
           comboBox1.DataSource = _db.Client();
           comboBox1.DisplayMember = "client"; //client names are shown in the combobox
        }


 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          comboBox2.SelectedItem =    ((DataRowView)comboBox1.SelectedItem).Row["gender"].ToString(); //the value of gender in database is put in the combobox2
        }

@@我的 Gender 组合框 (combobox2) 中的项目未绑定到数据库我只是手动将值放在组合框的项目属性中(这是 Visual Studio 中的一个功能)

非常感谢!如果您不清楚,请问我,我会添加一些细节。

【问题讨论】:

    标签: c# winforms data-binding combobox textbox


    【解决方案1】:

    尝试使用类似的东西:

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
     {
          String genderString = ((DataRowView)comboBox1.SelectedItem).Row["gender"].ToString();
    
          if (!String.IsNullOrEmpty(genderString))
          {
               comboBox2.SelectedItem = ((DataRowView)comboBox1.SelectedItem).Row["gender"].ToString(); //the value of gender in database is put in the combobox2
          }
          else
          {
               comboBox2.SelectedIndex = -1;
          }
     }
    

    如果性别为空字符串,它只是将组合框设置回未选中状态(我怀疑null 值不应显示为该列的值)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 2023-03-07
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      相关资源
      最近更新 更多