Multi-column sorting

Dec 5, 2008 at 1:53 PM
In this great article

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

He gives a way to speed up sorting performance, but it is at the expense of losing the Multi-column sorting capabilities of the grid.  ( Shift, header clicks ).  How is the Multi-Column sorting communucated to the developer and how can we support it but yet still use some of the techniques given in the link above?

Thanks
Paul
Coordinator
Dec 5, 2008 at 6:43 PM
Hi Paul,

    The default sort is able to do Multi-Column sort because it depends upon the SortDescriptions and one can get thier order from CollectionView's SortDescriptionCollection and use them for next sort. But this is not case with Custom Sort (in fact the perf improving factor of the custom sort from above link is not to use SortDescriptions). So technically since user (developer) has implemented the Sorting it would be his reponsibility to implement the MultiColumn Sort too.

Having said that, following is the approach....

  1. Create an Attached Property somewhere to track the order of sorts

public static readonly DependencyProperty ColumnSortOrderProperty =
    DependencyProperty.RegisterAttached("ColumnSortOrder",typeof(List<DataGridColumn>),<Your owning type>);

public static List<DataGridColumn> GetColumnSortOrder(DataGrid dataGrid)
{
    if(dataGrid == null)
    {
        throw new ArgumentNullException("dataGrid");
    }
    (List<DataGridColumn>)dataGrid.GetValue(ColumnSortOrderProperty);
}

public static void SetColumnSortOrder(DataGrid dataGrid, List<DataGridColumn> value)
{
    if(dataGrid == null)
    {
        throw new ArgumentNullException("dataGrid");
    }
    dataGrid.SetValue(ColumnSortOrderProperty, value);
}

    2.   Then modify your PerformCustomSort method and and MySort comparer such that they take advantage of this property accordingly. Some of the instructions of doing it would be...

    • Create the list if it is null and is needed.
    • Clear the list on the start of every single sort (Sort without pressing shift)
    • Do not clear it on MultiSort.
    • Modify your comprarer such that it honors the previous sort in the order defined by the property in case of MultiSort.
    • Use the SortDirection property on DataGridColumn.
    • Add the column to the list after every sort (single or multi).
Dec 11, 2008 at 4:54 PM
thanks. Off to visual studio to play.

Is there any easy way to force a sort programattically?

Without using the custom sorting, just a regular datagrid, if the user has selected two columns for sorting and I have to reload the data, how do I re-apply the sort programmatically?

When I reload the data, I lose the sorting.  Any clues?

Thanks
Paul
Coordinator
Dec 11, 2008 at 6:57 PM
If you are using Custom Sort, you can just call your PerformCustomSort method to force a sort programatically.

By reloading data, do you mean changing the ItemsSource itself or modifying the collection tied to ItemsSource. If it is the latter, one shouldnt lose sorting (if not custom sorting). If it is the former then you willl lose sorting (the reason and suggestions are in http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=41312).
Dec 11, 2008 at 9:36 PM
Thanks

the link helped.

Paul