When writing Winform programs, we encountered a conflict between the comboBox binding event and the index item change event, that is, the error "the item set cannot be modified after the DataSource attribute is set". We checked a lot online, most people say that the index item is changed to a non-empty judgment, and another guy adds a successful binding status to assist in the judgment, but I did find that it was not enough, later I met a dude who helped me solve the problem.
Problem description:
I want to implement the function of linking multiple drop-down lists. For example, there are three drop-down Lists A, B, and C. After selecting the data in drop-down list, data in drop-down list B changes accordingly. Select B in the drop-down list to pull data in list C. Of course, data is bound using the DataSource attribute of ComboBox. Result When I select drop-down list A, the system throws an exception "the item set cannot be modified after the DataSource attribute is set ".
Solution:
As a result, I had no satisfactory answers when I tried to find information online. There is no clear answer to the help manual. However, I can see from the manual the following sentence: "The object implementing the IList interface, such as DataSet or Array .", Pay attention to the red sentence. When the pull-down list items are cleared, the value of ComboBox. DataSource is "null", and the problem is solved ..
The code example is as follows:
Protected void BindArea (ComboBox combo_Area, int parentID)
{
{
Combo_Area.DataSource = null;
Combo_Area.Items.Clear ();
}
DataRow [] dList = areaList. Select ("ParentID =" + parentID. ToString ());
If (dList. Length> 0)
{
ArrayList da = new ArrayList ();
Foreach (DataRow dr in dList)
{
SysDic = new SysDic (dr );
Da. Add (dic );
}
Combo_Area.DisplayMember = "DicName ";
Combo_Area.ValueMember = "DicID ";
Combo_Area.DataSource = da;
}
}