C # solution of the ComboBox control "the item set cannot be modified after DataSource attribute is set,
Because I did not receive any professional questions after graduation, I did not touch the Winform program in year 45. Suddenly, due to work problems, I want to write small winform software for my own convenience. When using the ComboBox control, you may encounter a problem with re-binding the assignment.
The error code is as follows:
if (CustomerBLL.select().Rows.Count > 0) { cbTcid.Items.Clear(); cbTcid.DataSource = CustomerBLL.select(); cbTcid.ValueMember = "Cid"; cbTcid.DisplayMember = "Cpname"; } else { return; }
Under normal circumstances, when the data is re-assigned or bound to the data source, in order to prevent data problems, the original data will be cleared first, so this is the case, but there is no such thing as this. So I found it online. This is the case. [As follows]
The online search method is as follows:
if (CustomerBLL.select().Rows.Count > 0) { // cbTcid.Items.Clear(); if (cbTcid.Items.Count > 0) { cbTcid.DataSource = null; cbTcid.Items.Clear(); } cbTcid.DataSource = CustomerBLL.select(); cbTcid.ValueMember = "Cid"; cbTcid.DisplayMember = "Cpname"; } else { return; }
However, I finally tried to bind the data source next time without clearing the data of the previous time. Is that all right? So it succeeded.
if (CustomerBLL.select().Rows.Count > 0) { /*cbTcid.Items.Clear(); if (cbTcid.Items.Count > 0) { cbTcid.DataSource = null; cbTcid.Items.Clear(); }*/ cbTcid.DataSource = CustomerBLL.select(); cbTcid.ValueMember = "Cid"; cbTcid.DisplayMember = "Cpname"; } else { return; }
In addition, the data before the data source is bound is automatically cleared. Is that true or not? The ComboBox control is automatically cleared when it is bound next time. What about other controls? Are you welcome to discuss this and will the code in this method be nonstandard? Since I use small data, is it feasible to use big data? I hope you can talk about your suggestions.