更新级联组合框问题C#

时间:2011-08-11 10:09:41

标签: c# winforms cascade

我有一个简单的问题 我有一个包含两个相关组合框的表单,但我在更新第二个下拉内容时遇到问题

这是代码

  private void DiaryForm_Load(object sender, EventArgs e)
    {
       // TODO: This line of code loads data into the 'expensesDataSet.Item' table. You can move, or remove it, as needed.
      //  this.itemTableAdapter.Fill(this.expensesDataSet.Item);
        using (OleDbConnection con = dbconn.dbconnection())
        {
            ds1 = new ExpensesDataSet();

            string sql = "SELECT * From DiaryView";

            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
            da.Fill(ds1, "DiaryView");
            NavigateRecords();
            MaxRows = ds1.Tables["DiaryView"].Rows.Count;

            //fill category combobox
           string sqlcat = "SELECT * From Category";

           catda = new System.Data.OleDb.OleDbDataAdapter(sqlcat, con);
           catda.Fill(ds1, "Category");
           catda.Update(ds1, "Category");
           comboBox2.DataSource=ds1.Tables["Category"];
           comboBox2.DisplayMember = "cat_name";
           comboBox2.ValueMember="cat_id";

           //comboBox1.Enabled = false;

        }


    }

 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (OleDbConnection con = dbconn.dbconnection())
            {
                if (comboBox2.Items.Count > 0)
                {

                    {
                        string cat = comboBox2.SelectedValue.ToString();
                        comboBox1.Enabled = true;
                        int catid = int.Parse(comboBox2.SelectedValue.ToString());
                        string sqlitem = "SELECT * From Item where cat_id = " + catid;
                        catda = new System.Data.OleDb.OleDbDataAdapter(sqlitem, con);

                        this.itemBindingSource.EndEdit();


                        catda.Fill(ds1, "Item");
                        catda.Update(ds1, "Item");
                        comboBox1.DataSource = ds1.Tables["Item"];
                        comboBox1.DisplayMember = "item_name";
                        comboBox1.ValueMember = "item_id";


                    }
                }
            }

    }

有两个表: 类别(CAT_ID,cat_name) 项(ITEM_ID,ITEM_NAME,CAT_ID)

我该怎么办? PLZ帮助:)

1 个答案:

答案 0 :(得分:0)

如果要清除组合框,请执行以下操作:

combobox1.Items.Clear();

就这么简单。