CanUserSortColumns in datagrid not affected?

I have a datagrid with a binding source. I set the CanUserSortColumns datagrid property to TRUE and therefore with all the inner columns in the datagrid, but the user still cannot sort the columns.

Is there something I missed?

+4
source share
2 answers

Are you explicitly defining a DataTemplate for your headers? If so, you must set the property in the "SortMemberPath" column to your CLR object on which you want to sort the column. This link may be useful for you, look at it -

WPF4 Datagrid does not sort column headers

+3
source

Thanks guys. It worked. I just want to add.

The types of these columns should implement non-generic IComparable , which is usually not a problem if you use primitive or .net types. But if you have your own types, then you have to add them.

eg.

 /* this is my own type */ public struct Distance : ..., IComparable, IComparable<Distance>, ... { ... public int CompareTo(object obj) { if (obj == null) { return 1; } if (obj.GetType() != typeof(Distance)) { return 0; } return CompareTo((Distance)obj); } public int CompareTo(Distance other) { return _meters.CompareTo(other._meters); } } 
+1
source

All Articles