Saturday, March 28, 2009

Working with WPF DataGrid - Part 5

The DataGrid supports sorting by default on all bound and stock columns. You can set the CanUserSortColumns="False" if you want to disable sorting. For sorting template columns you need to set the SortMemberPath property of the columns to the property name of the business object that the column is bound to.

In my example I have enable sorting on the columns like

<WPF:DataGridTextColumn Binding="{Binding Path=LastName}" Header="LastName" Width="100" />

<WPF:DataGridTemplateColumn Header="Occupation" SortMemberPath="Occupation">

<WPF:DataGridTemplateColumn.CellEditingTemplate>

<DataTemplate>

<ComboBox SelectedItem="{Binding Path=Occupation, Mode=TwoWay}" ItemsSource="{Binding Source={StaticResource _occupationFactory}}" IsEditable="True" IsReadOnly="True" />

DataTemplate>

WPF:DataGridTemplateColumn.CellEditingTemplate>

<WPF:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<TextBlock Text="{Binding Path=Occupation}" />

DataTemplate>

WPF:DataGridTemplateColumn.CellTemplate>

WPF:DataGridTemplateColumn>

You can also have a look into Vincent’s blog where he explains the concept of tri-state sorting on WPF DataGrid. The next part of the series we’ll see how to filter data on DataGrid.

No comments: