簡體   English   中英

Visual C#相對動態的組合框

[英]Visual c# relatively dynamic comboboxes

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

我試圖在第一次選擇時將這段代碼放在SelectionChangeCommitted中,它填充了正確的項目,但是我的第二個選擇在comboBox2.Items.Clear();出錯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";
    }

當cbMun函數填充組合框時,它將直接調用組合框選擇的索引更改事件,因此要變通解決此定義bool值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";
        }
    }

我只是將代碼放置在comboBox1_SelectionChangeCommitted而不是放置在comboBox1_SelectedIndexChanged事件中,因為除非用戶將其更改提交給項目,否則comboBox1.SelectedValue仍然不會生成。 另外, Items collection cannot be modified when the DataSource property is set comboBox2.Items.Clear(); Items collection cannot be modified when the DataSource property is set錯誤Items collection cannot be modified when the DataSource property is set我的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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM