0

2つの関連するコンボボックスがあります。combobox1はcombobox2のアイテムにデータを入力します。combobox1selectIndexChangedイベントでこのコードがありますが、エラーが発生しますUnknown column 'System.Data.DataRowView' in 'where clause'

このコードを最初の選択に入れようとしましたSelectionChangeCommittedが、正しいアイテムが入力されていますが、2番目の選択では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";
    }
4

2 に答える 2

1

cbMun 関数がコンボボックスを埋めている間、コンボボックスの選択されたインデックス変更イベントを直接呼び出します。この問題を回避するには、bool 値をomboisloading = 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_SelectionChangeCommittedませんでした。また、私からの私のエラーは、このコード行を削除したばかりです。それでも、combobox2アイテムを適切にクリアして交換します。うーん..私はvbでコンボボックスをクリアするために使用したので..私は疑問に思います。comboBox1_SelectedIndexChangedcomboBox1.SelectedValueItems collection cannot be modified when the DataSource property is setcomboBox2.Items.Clear();

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 に答える