2013-03-19 80 views
0

我有兩個相關的組合框,combobox1填充combobox2的項目。在combobox1 selectIndexChanged事件我有這個代碼,但我有錯誤Unknown column 'System.Data.DataRowView' in 'where clause'

Visual c#相對動態組合框

我試圖把在SelectionChangeCommitted這段代碼的第一選擇,它填充了正確的項目,但我的第二選擇,我有錯誤comboBox2.Items.Clear();狀態Items collection cannot be modified when the DataSource property is set. :(做什麼?

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string sql; 
    if (comboBox1.SelectedIndex >= 0) 
    { 
     comboBox2.Items.Clear(); 
     MySqlConnection conn = new MySqlConnection(sqlString); 
     MySqlDataAdapter adapter = new MySqlDataAdapter(); 
     sql = "SELECT brgyname,idbrgy from barangay where idmun=" + comboBox1.SelectedValue; 
     adapter.SelectCommand = new MySqlCommand(sql, conn); 
     DataTable cbBrgy = new DataTable(); 
     adapter.Fill(cbBrgy); 
     comboBox2.DataSource = cbBrgy; 
     comboBox2.DisplayMember = "brgyname"; 
     comboBox2.ValueMember = "idbrgy"; 
    } 
} 

下面是我如何填充combobox1

private void cbMun() 
    { 
     MySqlConnection conn = new MySqlConnection(sqlString); 
     MySqlDataAdapter adapter = new MySqlDataAdapter(); 
     adapter.SelectCommand = new MySqlCommand("SELECT munname,idmun from municipality", conn); 
     DataTable cbMun = new DataTable(); 
     adapter.Fill(cbMun); 
     comboBox1.DataSource = cbMun; 
     comboBox1.DisplayMember = "munname"; 
     comboBox1.ValueMember = "idmun"; 
    } 

回答

0

我只是把我的代碼comboBox1_SelectionChangeCommittedcomboBox1_SelectedIndexChanged事件以來comboBox1.SelectedValue仍然不會生成,除非用戶對項目進行了更改。我的Items collection cannot be modified when the DataSource property is set錯誤comboBox2.Items.Clear();我剛剛刪除了這行代碼。我仍然清理並正確替換我的combobox2項目。嗯..因爲我曾經清除組合框中的VB ..我想知道。

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e) 
{ 
    string sql; 
     MySqlConnection conn = new MySqlConnection(sqlString); 
    MySqlDataAdapter adapter = new MySqlDataAdapter(); 
    sql = "SELECT brgyname,idbrgy from barangay where idmun=" + comboBox1.SelectedValue; 
    adapter.SelectCommand = new MySqlCommand(sql, conn); 
    DataTable cbBrgy = new DataTable(); 
    adapter.Fill(cbBrgy); 
    comboBox2.DataSource = cbBrgy; 
    comboBox2.DisplayMember = "brgyname"; 
    comboBox2.ValueMember = "idbrgy"; 
} 
1

而cbMun功能填充組合框就會直接調用下拉框選擇指數的變化情況,因此要解決這個定義布爾值comboisloading = TRUE,並修改代碼,如下圖所示:

private void cbMun() 
    { 
     MySqlConnection conn = new MySqlConnection(sqlString); 
     MySqlDataAdapter adapter = new MySqlDataAdapter(); 
     adapter.SelectCommand = new MySqlCommand("SELECT munname,idmun from municipality", conn); 
     DataTable cbMun = new DataTable(); 
     adapter.Fill(cbMun); 
     comboBox1.DataSource = cbMun; 
     comboBox1.DisplayMember = "munname"; 
     comboBox1.ValueMember = "idmun"; 
     comboisloading = false; 
    } 

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboisloading) 
      return; 

     string sql; 
     if (comboBox1.SelectedIndex >= 0) 
     { 
      comboBox2.Items.Clear(); 
      MySqlConnection conn = new MySqlConnection(sqlString); 
      MySqlDataAdapter adapter = new MySqlDataAdapter(); 
      sql = "SELECT brgyname,idbrgy from barangay where idmun=" + comboBox1.SelectedValue; 
      adapter.SelectCommand = new MySqlCommand(sql, conn); 
      DataTable cbBrgy = new DataTable(); 
      adapter.Fill(cbBrgy); 
      comboBox2.DataSource = cbBrgy; 
      comboBox2.DisplayMember = "brgyname"; 
      comboBox2.ValueMember = "idbrgy"; 
     } 
    }