WPF Datagrid group and sort

元气小坏坏 提交于 2019-12-30 07:24:09

问题


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 dept column. when I click on the dept column header I want to sort the grouped items.

Here I am using ListCollectionView to group the items in the code behind.

public  ListCollectionView collection;
collection = new ListCollectionView(obj.empData);

collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.Items.SortDescriptions.Add
          (new System.ComponentModel.SortDescription
                   ("Country"
                    ,System.ComponentModel.ListSortDirection.Descending
                   )
           );
dgData.Items.SortDescriptions.Add
          (new System.ComponentModel.SortDescription
                    ("Contact"
                     , System.ComponentModel.ListSortDirection.Descending
                    )
          );
dgData.ItemsSource = collection;

private void dgData_Sorting
        (object sender, Microsoft.Windows.Controls.DataGridSortingEventArgs e)
{
    if (e.Column.SortDirection.ToString() == "Ascending")
    {
        //dgData.Items.SortDescriptions.Clear();
        collection.Refresh();
        collection = new ListCollectionView(obj.empData);
        collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
        dgData.Items.SortDescriptions.Add
             ( new System.ComponentModel.SortDescription
                  ("Country"
                   , System.ComponentModel.ListSortDirection.Descending
                  )
              );
         dgData.ItemsSource = collection;
    }
}

After changing the sort order it is not reflecting in the UI. Please let me know the correct way to implement this.


回答1:


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



回答2:


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;
}



回答3:


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


来源:https://stackoverflow.com/questions/16050211/wpf-datagrid-group-and-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!