2011-08-11 75 views
0

我有一個簡單的問題 我有包含兩個相關組合框的形式,但我在更新第二個下拉內容更新層疊組合框問題C#

這是代碼

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(item_id,item_name,cat_id)

我該怎麼辦? plz幫助:)

+0

對不起,你不說*你的問題是什麼,你剛剛粘貼了你的代碼。 – Benjol

+0

如果您花費更多精力使用適當的大寫字母並避免txtspk,您還將增加獲得答案的機會。參見[寫出完美的問題](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx)。 – Benjol

+0

我想在填充之前清除combobox1 – marwa

回答

0

如果要清除組合框,只是做:

combobox1.Items.Clear(); 

就這麼簡單。