Click here to Skip to main content
15,920,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is c# winform and im using tabControl.
whenever the tabPage is loaded, the code will read the ID/data from sql and add them into the combobox:

C#
private void tpProduct_Enter(object sender, EventArgs e)
       {
           MySql.Data.MySqlClient.MySqlDataReader dr = runQuery("SELECT * FROM category");

           this.combBox1.Items.Clear();

           var dict = new Dictionary<string, string>();
           while (dr.Read())
           {
               dict.Add(dr["id"].ToString(), dr["name"].ToString());
           }
           combBox1.DataSource = new BindingSource(dict, null);
           combBox1.DisplayMember = "Value";
           combBox1.ValueMember = "Key";

           ConnectionClose();
       }

if i switch between the tab too fast, then this error message pop up "Item collection cannot be modified when datasource is set"

any idea how to solve this?
Posted

1 solution

before clearing the items in combobx, set its datasource to null.i.e.
C#
this.combBox1.DataSource =  null;
this.combBox1.Items.Clear();


source:Items collection cannot be modified when the DataSource property is set[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900