C # solution of the ComboBox control "the item set cannot be modified after DataSource attribute is set,

Source: Internet
Author: User

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.

 

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.