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!