Search code examples
c#sortingcomboboxbindingsource

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.Sort = "timesUsed ASC";
drpBox2.DataSource = bsAddresses.DataSource;
drpBox2.DisplayMember = "address";

How can I make it work?


Solution

  • 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";