WPF Datagrid group and sort

后端 未结 3 1179
轮回少年
轮回少年 2021-01-06 15:49

I am implementing grouping in WPF datagrid. I want to sort the grouped items. For example datagrid is having four columns(empno,name,dept,address). I am doing grouping by de

相关标签:
3条回答
  • 2021-01-06 16:16

    I found that turning on Live Sorting makes the second sort column actually take affect, e.g:

    collection.IsLiveSortingRequested = true;
    collection.LiveSortingProperties.Clear();
    collection.LiveSortingProperties.Add("Contact");
    
    0 讨论(0)
  • 2021-01-06 16:28

    Have you seen the MSDN article How to: Sort a GridView Column When a Header Is Clicked which refers to the sample from ListView That Sorts Data Sample and the latter has (Download sample) link

    Funny but the eventual reference to sample download is available only through .NET 3.0 and 3.5 version of the MSDN article but not through versions for .NET 4.0 and 4.5 though the code snippets are the same.

    There are also bog articles with samples based on above MSDN sample:

    • Thomas Levesque's [WPF] Automatically sort a GridView when a column header is clicked
    • Thomas Levesque's [WPF] Automatically sort a GridView (continued)
      The latter also has a code

    There is also MSDN blog articles with runnable Visual Studio project (with depemnency on WPF Toolkit) :

    • WPF DataGrid: Tri-state Sorting sample
    • Improving Microsoft DataGrid CTP sorting performance
    0 讨论(0)
  • 2021-01-06 16:34

    You can use this code (limitation : the "Country" order will reset to ascending when sorting on "Contact") :

    void dgData_Sorting(object sender, DataGridSortingEventArgs e)
    {
        // reset sortings
        collection.SortDescriptions.Clear();
    
        // define column sort
        e.Column.SortDirection = e.Column.SortDirection 
              == ListSortDirection.Ascending 
                 ? ListSortDirection.Descending : ListSortDirection.Ascending;
    
        // sort collection
        collection.SortDescriptions.Add
                 (new SortDescription
                       (e.Column.SortMemberPath
                        , e.Column.SortDirection.GetValueOrDefault()
                       )
                  );
    
        // mark event as handled otherwise the datagrid will "reset" your custom sorting
        e.Handled = true;
    }
    
    0 讨论(0)
提交回复
热议问题