1

How do I add a special sorting method to a particular column to allow different types of sorting(such as sorting 120.5.1.50 in between 120.5.1.12 and 120.5.1.110 instead of having 120.5.1.110 be the lowest value.

Also how do I allow click header sorting of a custom type bound it a template column. Is this even possible?

Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
  • This can help? http://stackoverflow.com/questions/2129601/how-can-i-apply-a-custom-sort-rule-to-a-wpf-datagrid – michele Mar 08 '13 at 11:35
  • possible duplicate of [WPF DataGrid CustomSort for each Column](http://stackoverflow.com/questions/18122751/wpf-datagrid-customsort-for-each-column) – Chris Nov 20 '14 at 17:11

2 Answers2

1

You can implement IComparer and define your own comparing logic.

public class MyComparer : IComparer<Object>
{

    public int Compare(Object stringA, Object stringB)
    {
         // Your logic here
    }
}

After you can just use LINQ OrderBy method with your custom comparer.

items = items.OrderBy(x => property, comparer).ToList();

Refer to this link.

Edit TO override the default sorting behaviour of a WPF Datagrid, refer to the answer in this link.

Community
  • 1
  • 1
failedprogramming
  • 2,532
  • 18
  • 21
0

If you want to maintain the custom sort order after clicking the column header, you can use an attached behaviour. I came up with this solution which seems to work well:

WPF DataGrid CustomSort for each Column

This is an MVVM solution - there are probably simpler ways of doing this if you want to delve into the world of code-behind.

Community
  • 1
  • 1
trilson86
  • 939
  • 1
  • 9
  • 20