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?

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

the question isn't about how to implement sorting but how to add sorting behaviour for wpd datagrid when you click on a column heading
Fair enough, I've updated my answer with a link. Look at the accepted answer
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.