Search code examples
wpfwpfdatagrid

Datagrid : Is there no Sorted event?


I need to know when a WPF Datagrid has been sorted by the user. Why is there no Sorted event? I can only find a Sorting event.

I also investigated the CollectionView and ListCollectionView that is exposing the objects to the View, without any luck.

I am quite surprised as this should come out of the box. Any ideas?


Solution

  • datagrid has "Sorting" event, subscribe to it!

    XAML:

    <DataGrid ItemsSource="{Binding YourItems}" AutoGenerateColumns="True" anUserSortColumns="True" 
               Sorting="DataGrid_Sorting"/>
    

    .cs code:

    private void DataGrid_Sorting(object sender, System.Windows.Controls.DataGridSortingEventArgs e)
    {
        Console.WriteLine(string.Format("sorting grid by '{0}' column in {1} order", e.Column.SortMemberPath, e.Column.SortDirection));
    }