WPF Setting Default Sort Order For Datagrid Without Using A Static Resource

For this you need a CollectionViewSource and bind the DataGrid ItemsSource to the View property of the CollectionViewSource.

The Basic Wire-Up
Andrea Boschin on Silverlight PlayGround demonstrates the basic wiring for Binding the DataGrid ItemsSource to the CollectionViewSource. Andrea is using MVVM and hence the CollectionViewSource is a property in his ViewModel class. I’ve excerpted it here:

Public Class SomeViewModel

// The View of this cvs will get bound to a datagrid
Public CollectionViewSource People { get; set; }

// The actual data that the cvs will construct its view out of
protected ObservableCollection(Of People) PeopleInternal { get; set; }
}
} //end class

In XAML, bind the DataGrid to the View of the People CollectionViewSource:

wpftoolkit:DataGrid ItemsSource="{Binding People.View}"
wpftoolkit:DataGrid.Columns
wpftoolkit:DataGridTextColumn Header="Name"
MinWidth="120" Binding="{Binding Path=Name, Mode=TwoWay}" /
wpftoolkit:DataGridTextColumn Header="Age"
Width="auto" Binding="{Binding Path=Age, Mode=TwoWay}" /
/dg:DataGrid.Columns
/wpftoolkit:DataGrid

Taking over from where Andrea left off, when the ObservableCollection is set, we initialise the CollectionViewSource:


protected ObservableCollection(Of People) PeopleInternal
{ get {return _peopleInternal };
set;
{
_peopleInternal = value
People.Source = _peopleInternal
}
}

Setting Default Sort Order
In order to establish the default Sort Order for the Datagrid, add a SortDescription to the CollectionViewSource that the DataGrid is bound to. This creates the required view.

Let’s say we wanted our Datagrid to have a sort order of ‘Name’, assuming ‘Name’ is a field on the People class.


protected ObservableCollection(Of People) PeopleInternal
{ get {return _peopleInternal };
set;
{
_peopleInternal = value
People.Source = _peopleInternal
// Setting Sort Order for People
// Sort by Name field
People.SortDescriptions.Clear()
SortDescription sd = new SortDescription("Name", ListSortOrder.Ascending)
People.SortDescriptions.Add(sd)
}
}

So, every time PeopleInternal is updated, the CollectionViewSource creates a view on PeopleInternal which is sorted by Name. This is held in People.View. Because the DataGrid is bound to this View the data in it is always sorted by name when the ItemsSource (the ObservableCollection(Of People) is initialised

This works around the behaviour in the WPF Toolkit DataGrid where all SortDescriptions for the DataGrid are lost when the ItemsSource is initialised

Of course, users will still be able to click on Column Headers and sort at any time on any other column they want to.

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: