How can I apply a custom sort rule to a WPF DataGrid?
Asked Answered
S

2

30

When the user does a column sort in my DataGrid, I want all null or empty cells to be sorted to the bottom, rather than the top.

I wrote an IComparer<T> that makes sure blanks are always sorted downward, but I can't figure out how to apply it to the columns of my DataGrid. Note that the initial sort of the DataGrid, which I'm doing with the LINQ OrderBy() method, works great. The problem is that all subsequent sorts performed by the user sort the blanks to the top.

Comparer Code

public class BlankLastStringComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y))
            return 1;
        else if (!string.IsNullOrEmpty(x) && string.IsNullOrEmpty(y))
            return -1;
        else
            return string.Compare(x, y);
    }
}

Questions

How do I get the DataGridColumn to use my comparer? Or if this is not possible, can you offer a workaround? I'm hoping for an MVVM friendly solution if possible.

Salian answered 25/1, 2010 at 0:51 Comment(1)
Could you this approach, quite flexible and easy to use. https://mcmap.net/q/500600/-wpf-datagrid-customsort-for-each-columnMagdalen
S
34

this is how i do it : I do derive from the grid to keep all of this inside the class, so i attach to event handlers internally

attach to the sorting event

dataGrid.Sorting += new DataGridSortingEventHandler(SortHandler);

implement the method (i do this in a derived class)

void SortHandler(object sender, DataGridSortingEventArgs e)
{
    DataGridColumn column = e.Column;

    IComparer comparer = null;

    //i do some custom checking based on column to get the right comparer
    //i have different comparers for different columns. I also handle the sort direction
    //in my comparer

    // prevent the built-in sort from sorting
    e.Handled = true;

    ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;

    //set the sort order on the column
    column.SortDirection = direction;

    //use a ListCollectionView to do the sort.
    ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(this.ItemsSource);

    //this is my custom sorter it just derives from IComparer and has a few properties
    //you could just apply the comparer but i needed to do a few extra bits and pieces
    comparer = new ResultSort(direction);

    //apply the sort
    lcv.CustomSort = comparer;
}
Sande answered 25/1, 2010 at 6:17 Comment(1)
Just a note that depending on the type of collection your DataGrid was bound to, you may have to cast the result of GetDefaultView to A Different Type.Ottavia
P
6

I have an MVVM solution for this problem which makes use of attached behaviours. If you prefer to use code-behind, @Aran's solution will do the trick too.

https://mcmap.net/q/500600/-wpf-datagrid-customsort-for-each-column

Pieria answered 28/10, 2013 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.