【问题标题】:A Combobox Population issue组合框人口问题
【发布时间】:2025-12-04 04:30:01
【问题描述】:

我正在使用以下函数来填充我的ComboBox

 public static void FillDropDownList(string Query, System.Windows.Forms.ComboBox DropDownName, string AValue, string Adisplay)
    {
        string Value = AValue;
        string display = Adisplay;
        using (var CONN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Test.accdb;"))
        {
            CONN.Open();
            DataTable dt = new DataTable();
            try
            {
                OleDbCommand cmd = new OleDbCommand(Query, CONN);
                OleDbDataReader myReader = cmd.ExecuteReader();
                dt.Load(myReader);
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.ToString());
                return;
            }
            DropDownName.DataSource = dt;
            DropDownName.ValueMember = Value;
            DropDownName.DisplayMember = display;
        }
    }

我在加载表单中调用它:

 private void Form1_Load(object sender, EventArgs e)
 {
     FillDropDownList("select CountryCode,Countryname from Countries", comboBox1, "CountryCode", "Countryname");

 }

2.之后,我在 Selectedindexchanged 事件中使用相同的函数来填充另一个 ComboBox:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{            
    FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");

}   

对于两个组合框的人口来说,一切都很好,但问题就在这里 如图所示,我有两个组合框:

所以第一个组合框(国家)完美加载到表单中 但是我要做的是仅在我选择国家/地区时才从数据库中选择第二个组合框( state )但是发生的情况是代码运行并且第二个组合框会从数据库中选择所选择的内容第一个组合中的索引如图所示

所以我基本上想要做的是只有当我从第一个组合框中选择一个索引时才填充第二个组合。

【问题讨论】:

    标签: c# winforms combobox


    【解决方案1】:

    我认为您的问题是由于第一个组合框的填充触发事件以填充第二个组合框。解决它的一种方法是在 comboBox1_SelectedIndexChanged(object sender, EventArgs e) 中有类似以下内容。

    if (comboBox1.Text == string.Empty) { return; }
    

    如果这不起作用,请尝试使用“SelectedIndex > 0”或“SelectedValue”属性。

    请注意,如果有人能够修改您的组合框项,您的代码可能容易受到 SQL 注入攻击。

    【讨论】:

      【解决方案2】:

      把你的代码放在SelectionChangeCommitted而不是SelectedIndexChanged

      private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
      {
          FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");
      }
      

      【讨论】:

        【解决方案3】:

        您需要使用SelectedIndex 属性来识别该项目是否真的从 Combobox 更改。

        试试这个:

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {   
        
            if(comboBox1.SelectedIndex >= 0)
            {   
               FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");
            }
        
        }   
        

        【讨论】:

          【解决方案4】:

          将您的以下方法更改为

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
          {            
              FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");
          
          }  
          
                      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
                  {
                      if (comboBox1.SelectedIndex != -1 && comboBox2.SelectedIndex != -1 )
                      {
                          FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");
                      }
          
                  } 
          

          在提交查询数据库之前,您验证您的两个组合框。检查任何选定的项目。

          谢谢 斯里坎特

          【讨论】:

            【解决方案5】:

            在调用FillDropDownList 方法之前删除SelectedIndexChanged 事件处理程序,并在填充ComboBox 后重新应用它。

            private void Form1_Load(object sender, EventArgs e)
            {
              comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
              FillDropDownList("select CountryCode,Countryname from Countries", comboBox1, "CountryCode", "Countryname");
              comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
            }
            

            【讨论】: