Datagrid : Is there no Sorted event?
Asked Answered
G

4

9

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?

Glasscock answered 5/3, 2012 at 17:32 Comment(5)
MSDN may be a better place to ask "why".Gut
Handle Sort and just pass sort to the DataGrid.Arriola
handle sort? You mean sorting?Glasscock
Please take a look at this post. [Sorted Event][1] [1]: #8417461Semiweekly
This question is similar to: How can I be notified if a DataGrid column is sorted (and not sorting). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.Wardrobe
S
2

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));
}
Stave answered 2/4, 2012 at 22:14 Comment(1)
This is not the correct answer. OP specifically says he wants a Sorted event, not a Sorting event. The difference is whether the items have already been sorted. Oliver Dufner's comment pointing to a duplicate question is the correct response.Baronet
A
5

I've taken an example from MSDN documentation and adjusted it to raise a Sorted event when the Sorting event is done.

public class CustomDataGrid : DataGrid
{
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent SortedEvent = EventManager.RegisterRoutedEvent(
        "Sorted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomDataGrid));
    
    // Provide CLR accessors for the event
    public event RoutedEventHandler Sorted
    {
        add { AddHandler(SortedEvent, value); }
        remove { RemoveHandler(SortedEvent, value); }
    }

    // This method raises the Sorted event
    void RaiseSortedEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomDataGrid.SortedEvent);
        RaiseEvent(newEventArgs);
    }

    protected override void OnSorting(DataGridSortingEventArgs eventArgs)
    {
        base.OnSorting(eventArgs);
        RaiseSortedEvent();
    }
}

Then you can use it either in codebehind.

datagrid.Sorted += new RoutedEventHandler(datagrid_Sorted);

or in XAML

<local:CustomDataGrid x:Name="datagrid" Sorted="datagrid_Sorted"/>

And here the method that will get triggered when the datagrid finishing sorting:

private void datagrid_Sorted(object sender, RoutedEventArgs args)
{
     var datagrid = (CustomDataGrid)sender;
     var sortedItems = datagrid.Items;

}
Antipasto answered 1/3, 2017 at 20:44 Comment(0)
H
5

You can still subscribe to the DataGrid Sorting Event:

<local:CustomDataGrid x:Name="datagrid" Sorting="datagrid_Sorted;"/>

but to make sure that your actions happen after the sorting is done use Dispatcher :

private void DataGrid_Sorting(object sender, DataGridSortingEventArgs e)
{

    this.Dispatcher.BeginInvoke((Action)delegate()
    {
        //runs after sorting is done
    }, null);
}

This way, there's no need of a custom Datagrid class.

Huber answered 11/10, 2018 at 7:8 Comment(1)
this is a very simple and great solution, it works fine, thanksKeg
S
2

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));
}
Stave answered 2/4, 2012 at 22:14 Comment(1)
This is not the correct answer. OP specifically says he wants a Sorted event, not a Sorting event. The difference is whether the items have already been sorted. Oliver Dufner's comment pointing to a duplicate question is the correct response.Baronet
H
0

You could use the Dispatcher to ask if the Sorting Event is Finished.

private void datagrid_Sorting(object sender, DataGridSortingEventArgs e)
{
    this.Dispatcher.BeginInvoke((Action)(() =>
    {
        //Will be executed after the datagrid has finished sorting
    }));
}
Hydromancy answered 17/9, 2024 at 12:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.