C # combobox Control "Set DataSource property Cannot modify item collection" Perfect solution _c# Tutorial

Source: Internet
Author: User
Tags error code

Because after graduation work did not receive the professional question, causes four or five years has not touched the WinForm procedure. Suddenly because of work problems, in order to facilitate their own, so want to write their own small WinForm small software, used for their own use. When you use the ComboBox control, you experience a problem with a rebind 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
;
}

Normally, when data is assigned to or bound to a data source, in order to prevent data problems, the original data will be emptied first, so it is written, but there is no quite exactly this writing on the problem. So I looked it up on the Internet. Found that someone was doing this. Following

Online search methods are 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
;
}

But in the end I tried to, at the next time binding the data source, do not empty the last data, is it OK? So it worked.

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

And most of all, the data before the data source is bound is automatically emptied. Is it really so, or what? Is this combobox control automatically emptied the next time it is bound, and what about the other controls? Welcome to discuss, and this method will appear code is not standard? Because I am small data, it is possible to encounter large data, I hope you can talk about their own suggestions.

The above is a small set to introduce the C # ComboBox control "setting DataSource properties can not modify the item set" Perfect solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.