views:

628

answers:

3

I have a DataGrid in WPF app with several columns, including a Name column. If the users switches to a particular view, I want the data to be pre-sorted by Name (and I'd like a sort arrow to appear in the Name header just as if the user had clicked that header). However, I can't find the expected properties to make this happen. I was looking for something like SortColumn, SortColumnIndex, SortDirection, etc.

Is it possible to specify the default sort column and direction in markup (XAML) or is that not supported by the WPF Toolkit DataGrid?

+3  A: 

Assuming you're talking about the WPF Toolkit DataGrid control, you need only set the CanUserSortColumns property to true and then set the SortMemberPath property of each DataGridColumn in the DataGrid.

As far as sorting the collection initially, you must use a CollectionViewSource and set the sort on that and then assign that as the ItemsSource of your DataGrid. If you're doing this in XAML then it would be as easy as:

<Window.Resources>
    <CollectionViewSource x:Key="MyItemsViewSource" Source="{Binding MyItems}">
        <CollectionViewSource.SortDescriptions>
           <scm:SortDescription PropertyName="MyPropertyName"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</Window.Resouces>

<DataGrid ItemsSource="{StaticResource MyItemsViewSource}">

</DataGrid>

NOTE: the "scm" namspace prefix maps to System.ComponentModel where the SortDescription class lives.

Drew Marsh
@Drew, thanks but the `SortMemberPath` just specifies which field in the data source goes with which column in the DataGrid. I need to set the *current* sort column (and direction). E.g., this DataGrid is currently sorted ascending by Name.
DanM
Well that how you solve your "clicking the header column and sorting problem". I accidentally left out how to initially sort the grid, I will add to my answer now.
Drew Marsh
Yes :) That's much closer to what I was looking for, thanks. The only problem is that the sort arrow doesn't appear in the header of the column the table is sorted by. I can live with that, but an arrow would make it a little clearer to the user what the sort column is. Just a note for anyone trying to do this, you need a reference to `WindowsBase` to use `System.ComponentModel`. Once you've added the reference, you need this: `xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"`.
DanM
I had to use <DataGrid ItemsSource="{Binding Source={StaticResource MyItemsViewSource}}"> to get this to work.
Samuel Jack
A: 

When you see ItemsSource doesn't support CollectionViewSource exception, you can sort collection by Linq before you refer it to a DataGrid:

ObservableCollection<MyDataClass> myCollection = new ObservableCollection<MyDataClass>();
dataGrid.ItemsSource = from item in myCollection orderby item select item;

You have to implement IComparable interface to MyDataClass:

public class MyDataClass : IComparable<MyDataClass> {
    public int CompareTo(Classified other) {
        return other.Value.CompareTo(this.Value); // DESC
        return this.Value.CompareTo(other.Value); // ASC
    }
}
vasek7
A: 

When you see ItemsSource doesn't support CollectionViewSource exception then you can set the DataContext of DataGrid as 'MyItemsViewSource' and itemsSource as {Binding} like this:

Thanks

Sukhjeet