Search code examples
wpfentity-frameworkentity-framework-4wpfdatagrid

Datagrid Sorting (Still) Not Working with Entity Framework 4.0


I'm having trouble with an application I'm writing in VS2010. I need to display databound data on a Datagrid and allow the user to sort its contents.

This is my situation:

  1. I've built a very simple SQL database with 1 table named "Question". The table has 2 columns, "id_Question" and "QuestionText".

  2. I created an Entity model from this database.

  3. Under "Data Sources", I've dragged and dropped the "Question" Entity as a Datagrid onto my WPF window. When run, the Datagrid populates normally with whatever is in the database. Great.

  4. I've set the "CanUserSortColumns" and "CanUserSort" properties of the DataGrid and DataGrid columns to True. When the column header is clicked, nothing happens, nothing sorts.

Here is the XAML snippet:

  <DataGrid AutoGenerateColumns="False" 
              EnableRowVirtualization="True" Height="200" 
              HorizontalAlignment="Left" ItemsSource="{Binding}" 
              Margin="871,126,0,0" Name="questionsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" 
              VerticalAlignment="Top" Width="400"
              CanUserSortColumns="True">
        <DataGrid.Columns>
            <DataGridTemplateColumn x:Name="id_QuestionColumn1" Header="id Question" Width="SizeToHeader">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=id_Question}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="questionTextColumn1" 
                                Binding="{Binding Path=QuestionText}" Header="Question Text" Width="300"
                                CanUserSort="True"/>
        </DataGrid.Columns>
    </DataGrid>

I have read the following among many other related articles / questions on Stack Overflow and other sites:

Each has provided useful insights but I haven't been able to get it to work for my problem. What do I need to write in the code behind file to get this to work properly?

Thanks


Solution

  • Is your template column not sorting or ANY column is not sorting?

    If its issue with template column thenn ...

    Set

     DataGridTemplateColumn.SortMemberPath="SomeSortableProperty"
     DataGridTemplateColumn.SortDirection="Ascending"