Sorting Combobox with BindingSource.Sort, whats wrong here:
Cant get the code below to sort the Combobox (drpBox2) correctly.
BindingSource bsAddresses = new BindingSource();
bsAddresses.DataSource = searchedAddr;
bsAddresses.Sor开发者_如何学编程t = "timesUsed ASC";
drpBox2.DataSource = bsAddresses.DataSource;
drpBox2.DisplayMember = "address";
How can I make it work?
There is a simple error in the code, when using a BindingSource you must bind your Controls to the BS directly, not to its DataSource. In your Code, both bsAddresses
and drpBox2
are bound to searchedAddr
so the sorted BindingSource is not used at all. Fixed code:
BindingSource bsAddresses = new BindingSource();
bsAddresses.DataSource = searchedAddr;
bsAddresses.Sort = "timesUsed ASC";
drpBox2.DataSource = bsAddresses;
drpBox2.DisplayMember = "address";
There could be two issues here
- The Column required is case sensitive, so you should be supplying it in the proper case(ascending is default)
- Also for the underlying List to get sorted it must implement
IBindingList
(If not thenSupportsSorting
property would be false, indicating that source doesn't support sorting)
Read Here
精彩评论