Ikong
Ikong

Reputation: 2570

Visual c# relatively dynamic comboboxes

I have two related comboboxes, combobox1 populate the items of combobox2. In combobox1 selectIndexChanged event I have this code but i have error Unknown column 'System.Data.DataRowView' in 'where clause'.

I tried to put this code in the SelectionChangeCommitted at first selection, it populate the right items, but my second selection i have error in comboBox2.Items.Clear(); states Items collection cannot be modified when the DataSource property is set. :( what to do.?

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

Here's how i populate 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";
    }

Upvotes: 0

Views: 1074

Answers (2)

M.A
M.A

Reputation: 57

while cbMun function is filling the combobox it will call combobox selected index change event directly , so to work around this define bool value comboisloading = true and modify your code such like below :

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

Upvotes: 1

Ikong
Ikong

Reputation: 2570

I just placed my code in comboBox1_SelectionChangeCommitted not in comboBox1_SelectedIndexChanged event since comboBox1.SelectedValue is still not generated not unless the user committed its changes to the items. Also my Items collection cannot be modified when the DataSource property is set error from comboBox2.Items.Clear(); i just deleted this line of code. I still clears and replace my combobox2 items properly. Hmm.. Since i used to clear comboboxes in vb..I wonder.

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

Upvotes: 0

Related Questions