none
Sort BindingList

    Question

  • Hi,

    May i know how to sort a BindingList?
    Now i am assign a result from LINQ to BindingList, after that i will add in new item to the bindingList.
    At last, i want to sort the BindingList because of previously added item might affect sorting.

    I tried to do this, but i cannot convert it back to BindingList.
    bindinglist.OrderBy(a => a.ProdTransaction.TransactionDate)

    At the end, i need to bind the final sorted result back to data grid which allow add, edit, and delete functions.

    Do you have any solution to sort a BindingList?


    • Edited by lawrencewong Wednesday, October 02, 2013 1:38 AM
    Tuesday, October 01, 2013 10:26 AM

Answers

  • Hello,

    The Enumberable.OrderBy method returns an IOrderedEnumberable<T>, so we need to create a new BindingList<T> from that return value.

    Here is a quick sample:

                BindingList<Company> companyList = new BindingList<Company>
    
                {
    
                    new Company { CompanyName = "Company B" },
    
                    new Company { CompanyName = "Company C" },
    
                    new Company { CompanyName = "Company A" },
    
                };
    
                List<Company> sortedList = companyList.OrderBy(x => x.CompanyName).ToList();
    
                companyList = new BindingList<Company>(sortedList);
    

    We can also use two anonymus methods: Sort or OrderBy:

    For the example without create a new variable to order you have to use Sort method:

    companyList.Sort((a, b) => string.Compare(a.CompanyName, b.CompanyName));

    Or using OrderBy, where you have to create a new variable if you want to store ordered items into:

    List<Company> sortedList = companyList.OrderBy(x => x.CompanyName).ToList();

    And I think it will be a better option to customize our own sortable BindingList<T> type, and finish the tasks which asked in the ApplySortCore API document Remark section.

    There's also a sample code:

    http://www.martinwilley.com/net/code/forms/sortablebindinglist.html

    Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by lawrencewong Thursday, October 03, 2013 1:57 AM
    Wednesday, October 02, 2013 1:58 AM
    Moderator

All replies

  • Hello,

    The Enumberable.OrderBy method returns an IOrderedEnumberable<T>, so we need to create a new BindingList<T> from that return value.

    Here is a quick sample:

                BindingList<Company> companyList = new BindingList<Company>
    
                {
    
                    new Company { CompanyName = "Company B" },
    
                    new Company { CompanyName = "Company C" },
    
                    new Company { CompanyName = "Company A" },
    
                };
    
                List<Company> sortedList = companyList.OrderBy(x => x.CompanyName).ToList();
    
                companyList = new BindingList<Company>(sortedList);
    

    We can also use two anonymus methods: Sort or OrderBy:

    For the example without create a new variable to order you have to use Sort method:

    companyList.Sort((a, b) => string.Compare(a.CompanyName, b.CompanyName));

    Or using OrderBy, where you have to create a new variable if you want to store ordered items into:

    List<Company> sortedList = companyList.OrderBy(x => x.CompanyName).ToList();

    And I think it will be a better option to customize our own sortable BindingList<T> type, and finish the tasks which asked in the ApplySortCore API document Remark section.

    There's also a sample code:

    http://www.martinwilley.com/net/code/forms/sortablebindinglist.html

    Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by lawrencewong Thursday, October 03, 2013 1:57 AM
    Wednesday, October 02, 2013 1:58 AM
    Moderator
  • Thank You!!! Gracias!!
    Saturday, November 02, 2013 6:28 PM
  • Thank You!!
    Saturday, November 02, 2013 6:28 PM