none
How to do the manual sorting and filtering using the ListCollectionView in WPF DataGrid

    Question

  • Hi,

    I am new to WPF. I have binded listCollectionView to my WPF DataGrid as below

    Example.

    C#:

    List<I_Account> acclist;
    ListCollectionView acclistview;

    acclistview = new ListCollectionView(acclist);
    DataGrid1.DataContext = acclistview ;

    Here, I_Account is an interface, it has properties like, Id, name, etc..

    Please anyone help me with some code, how to do sorting and filtering manually in the WPF DataGrid using ListCollectionView.

    Wednesday, October 28, 2009 9:44 AM

Answers

All replies

  • hi
    try for below link

    http://weblogs.asp.net/monikadyrda/archive/2009/08/31/wpf-listcollectionview-for-sorting-filtering-and-grouping.aspx
    Harshad..... Always 4 U
    Wednesday, October 28, 2009 11:49 AM
  • But i hav already chceked this site but i got comfuced in writing Comparer class dynamically for List<I_Account> acclist.

    thats why i have asked question.

    Please help on how to do this with List<I_Account> acclist
    Wednesday, October 28, 2009 12:11 PM
  • Hello ksvimal,

      In addition to harjohn's,

       You can then apply the filter, as shown in the following example. In this example, acclistview is a ListCollectionView object.

       acclistview.Filter = new Predicate<object>(Contains);

       'Contains' is a mothed returns whether an Item(I_Account) should be chosen.
    public bool Contains(object de)
    {
        I_Account accountTest = de as I_Account;
    
        //Return all account Filled property equals No.
    
        //Others are not in the list.
    
        return(accountTest.Filled== "No");
    }
     Sort:
     
       We can add a SortDescription to the collection of SortDescription objects which is returned by SortDescriptions property of ListCollectionView

        For example:
    SortDescriptions.Add(
            new SortDescription("Content", ListSortDirection.Descending));
      Related links:

       http://msdn.microsoft.com/en-us/library/ms752348.aspx

       http://msdn.microsoft.com/en-us/library/ms754073.aspx

       http://msdn.microsoft.com/en-us/library/system.windows.data.listcollectionview.aspx


       If you have any question please let me know.

     Thanks.
    Please mark the replies as answers if they help and unmark them if they provide no help
    • Edited by Hua Chen Wednesday, October 28, 2009 12:53 PM Edit..
    • Marked as answer by Hua Chen Wednesday, November 04, 2009 2:45 AM
    Wednesday, October 28, 2009 12:51 PM
  • Hi Chen,

    Thanks for your valuable feedback.

    You solution works fine.

    Filter:
    Still i have some problem in generalizing the filter.

    return(accountTest.Filled== "No");

    In the above line, you have hard coded the property name "Filled".

    But for me, the filter property changes dynamically. So i want to have some like

    return
    (accountTest["Filled"]== "No");

    So, I can change the property name "Filled" with some other name based on which property i needed to filter it.

    Please give me some solution on this issue.


    Sorting:
    Instead of SortDescription, Can you please give some example for CustomSort. I feel CustomSort is much faster than SortDescription.

    Can you please give some example for CustomSort?

    Thursday, October 29, 2009 6:05 AM
  • Hello ksvimal

     But for me, the filter property changes dynamically. So i want to have some like

       return
    (accountTest["Filled"]== "No");

      If we want to use this syntax, the accountTest class need to implement the string index returning a string.

      I am not sure what difficulty you have meet.

      Can you please give some example for CustomSort?
     
       Here is an good article:

      http://blogs.msdn.com/jgoldb/archive/2008/08/26/improving-microsoft-datagrid-ctp-sorting-performance.aspx

      Good luck.
    Please mark the replies as answers if they help and unmark them if they provide no help
    • Edited by Hua Chen Thursday, October 29, 2009 7:10 AM Edit..
    Thursday, October 29, 2009 7:10 AM
  • Hi Chan,


    Thanks for your feedback again.

    1)
    Usually if i have 10 properties in accountTest, then i have return 10 statments to filter based on different properties

    Ex:

    return(accountTest.AccountId== "AY00001");
    return(accountTest.Filled== "No");
    etc....

    In this case, I just want to write single statement where property name changes dynamically.

    Is this possible??? Please Correct me if i am wrong.


    2)

    How to do filter based on properties at a time????
    Ex:
    accountTest.AccountId== "AY00001" AND accountTest.Filled== "No"


    Thursday, October 29, 2009 7:20 AM
  • Hello ksvimal,

      Does the following statement work for you 

       return ( accountTest.AccountId== "AY00001" || accountTest.Filled== "No") C# code

       Any condition meets. the accountTest is in the list.

       Thanks.
    Please mark the replies as answers if they help and unmark them if they provide no help
    Thursday, October 29, 2009 10:29 AM
  • Hi Chen,

    Thanks for your feedback.

    Your solution works for the 2nd issue. Actually i already found it. thanks

    Can you please give some solution for the FIRST issue??

    I will be grateful if there is any solution

     

     

     

    Thursday, October 29, 2009 10:42 AM