0

我有两个相关的组合框,组合框 1 填充组合框 2 的项目。在 combobox1selectIndexChanged事件中,我有这个代码,但我有错误Unknown column 'System.Data.DataRowView' in 'where clause'

我尝试将此代码放在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";
    }
}

这是我填充组合框1的方式

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

2 回答 2

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";
        }
    }
于 2013-03-19T13:14:09.120 回答
0

我只是将我的代码放在了comboBox1_SelectionChangeCommittednot in comboBox1_SelectedIndexChangedevent 中,因为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";
}
于 2013-03-19T06:59:29.637 回答