Making a DataGrid Column Header sortable in WPF using C#

Chuck McMullen picture Chuck McMullen · Aug 18, 2009 · Viewed 46.8k times · Source

I am using C# in Visual Studio 2008 and I have install the WPF Toolkit. I created a DataGrid in testtest.xaml. The ID and Parts $ columns have the ability to sort the DataGrid by clicking on their respecteive column headers. However, the column header Complete Date does not have that ability. I used the tag "DataGridTemplateColumn" to format the dates for this column. How do you program the column header Complete Date so you can click on the Complete Date column header and sort that column. If you click on the column, the arrow is not only not displayed but the column header is not "clickable". Thank you

    <Label Height="22" HorizontalAlignment="Left" Margin="10,45,0,0" Name="label1" VerticalAlignment="Top" Width="41">Task</Label>
    <my:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="140,83,67,28" Name="dataGrid1" GridLinesVisibility="Vertical" IsReadOnly="True">
        <my:DataGrid.Columns>
            <my:DataGridTextColumn    Binding="{Binding Path=[ID]}" Header="ID" />
            <my:DataGridTextColumn Binding="{Binding Path=p}" Header="Parts $" />
            <my:DataGridTemplateColumn  SortMemberPath="" Header="Complete Date">
                <my:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <Binding Path="CompleteDate" ConverterCulture="en-GB" StringFormat="{}{0:MM/dd/yyyy}"/>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>
        </my:DataGrid.Columns>
    </my:DataGrid>
</Grid>

Answer

Philip Rieck picture Philip Rieck · Aug 18, 2009

In your DataGridTemplateColumn you have SortMemberPath set to "". If you set this to an actual property on the item (say, CompleteDate), you should be able to sort. You can also set CanUserSort="true" or CanUserSort="false" on selected columns.

SortMemberPath gives the property to sort on when the user attempts a sort. If this isn't set, then the grid doesn't know how to sort that column ( it does not use the text in the column)

            <my:DataGridTemplateColumn  SortMemberPath="CompleteDate" Header="Complete Date" CanUserSort="true">
            <my:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <Binding Path="CompleteDate" ConverterCulture="en-GB" StringFormat="{}{0:MM/dd/yyyy}"/>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>