Solution to the ComboBox control "the item set cannot be modified after DataSource attribute is set"

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.