3
votes

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.

3

3 Answers

3
votes

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:

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

1
votes

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
votes

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