Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CanUserSortColumns in datagrid doesn't make effect?

I have a datagrid with binding item source. I have set CanUserSortColumns property of datagrid into TRUE and so do with all inner columns in datagrid but user still doesn't be able to sort columns.

Is there something I have missed ?

like image 674
JatSing Avatar asked Oct 26 '11 03:10

JatSing


2 Answers

Are you explicitly defining DataTemplate for your headers? In case yes you have to set property on your column "SortMemberPath" to your CLR property on which you want to sort your column. This link might prove helpful to you, have a look at it -

WPF4 Datagrid doesn't sort on column headers

like image 64
Rohit Vats Avatar answered Nov 03 '22 10:11

Rohit Vats


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

The types of those columns must implement the non-generic IComparable, which is usually not a problem if you are using primitive or .net types. But if you have your own types, then you will have to add it.

E.g.

/* 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); }
}
like image 1
Martin Lottering Avatar answered Nov 03 '22 10:11

Martin Lottering