We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to sort multiple column without pressing Ctrl key in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid
Tags: sorting

WPF DataGrid (SfDataGrid) allows you to perform the multiple sorting without pressing the Ctrl Key. You can achieve this by using the SortColumnsChanging event which will be raised while clicking on the column header. You should cancel the current sorting process and need to add the new sort column to SorColumnDescriptions using a Dispatcher.

In WPF

void SfdataGrid_SortColumnsChanging(object sender, GridSortColumnsChangingEventArgs e)
{
    e.Cancel = true;
    if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
    {
        this.SfdataGrid.Dispatcher.BeginInvoke(new Action(() =>
        {
            this.SfdataGrid.SortColumnDescriptions.Add(e.AddedItems[0]);
        }), DispatcherPriority.ApplicationIdle);
 
    }
    else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
    {
        this.SfdataGrid.Dispatcher.BeginInvoke(new Action(() =>
        {
            var sordesc = this.SfdataGrid.SortColumnDescriptions.FirstOrDefault(sd => sd.ColumnName == e.AddedItems[0].ColumnName);
            this.SfdataGrid.SortColumnDescriptions.Remove(sordesc);
            this.SfdataGrid.SortColumnDescriptions.Add(e.AddedItems[0]);
        }), DispatcherPriority.ApplicationIdle);
 
    }
}

 

In UWP

private async void SfdataGrid_SortColumnsChanging(object sender, Syncfusion.UI.Xaml.Grid.GridSortColumnsChangingEventArgs e)
{
    e.Cancel = true;
    if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
    {
        await this.SfdataGrid.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            this.SfdataGrid.SortColumnDescriptions.Add(e.AddedItems[0]);
        });
    }
    else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
    {
        await this.SfdataGrid.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            var sordesc = this.SfdataGrid.SortColumnDescriptions.FirstOrDefault(sd => sd.ColumnName == e.AddedItems[0].ColumnName);
            this.SfdataGrid.SortColumnDescriptions.Remove(sordesc);
            this.SfdataGrid.SortColumnDescriptions.Add(e.AddedItems[0]);
        });
    }
}

 

Applied sorting to multiple columns in WPF DataGrid

View WPF DataGrid Sorting Demo in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile