开发者

update Cascading combobox problem C#

i have a simple question i have a form which contain two related combo boxes but i have problem in updating the second dropdown content

this is the code

  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";


                    }
                }
            }

    }

there is two tables: category(cat_id,cat_name) item(i开发者_如何学Ctem_id,item_name,cat_id)

what can i do?? plz help :)


If you want to clear the combobox, just do:

combobox1.Items.Clear();

It's as easy as that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜