Sort by multiple columns automatically (like a LINQ sort)

Ok, so I understand that I can use CTRL to sort on multiple columns but I want to be able to apply a sort like a LINQ command such as OrderBy(....).ThenBy(...) - is this possible?

So for example, say I have a grid showing transactions including an Amount column and a Transaction Date column. When the users click the Amount column header they should see the data sorted by that column then by transaction date without having to add the extra column or click CTRL etc.  So what I want to do for some of the columns is add this specific sort order.

I am assuming this possible but have not seen anywhere showing how to do that - can we catch a sorting event and supply multiple columns to sort on (and the direction) or would I have to manually sort my ItemsSource or ViewModel data myself?

Sorry if this has been answered before - I'm very new to sfDataGrid and really like it but am still trying to work out how to do things that I can easily do in the normal WPF grid.

Cheers,

Mike

1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team November 2, 2020 04:16 PM UTC

Hi Mike Christie,

Thank you for contacting Syncfusion support.

Your requirement can be achieved by adding the SortColumnDescription in SfDataGrid.SortColumnDescriptions property in SortColumnsChanged event in SfDataGrid.
Please refer the below code snippet for your reference, 
sfDataGrid.SortColumnsChanged += SfDataGrid_SortColumnsChanged;

private void SfDataGrid_SortColumnsChanged(object sender, GridSortColumnsChangedEventArgs e)
 
{ 
        //when Click the UnitPrice column to apply sorting in this case at same time sorting applied for OrderID Column 
        if (e.AddedItems[0].ColumnName == "UnitPrice" && e.AddedItems[0].SortDirection == ListSortDirection.Ascending) 
        { 
                this.sfDataGrid.SortColumnDescriptions.Add(new SortColumnDescription() { ColumnName = "OrderID", SortDirection = ListSortDirection.Ascending }); 
        } 
        else if (e.AddedItems[0].ColumnName == "UnitPrice" && e.AddedItems[0].SortDirection == ListSortDirection.Descending) 
         { 
                this.sfDataGrid.SortColumnDescriptions.Add(new SortColumnDescription() { ColumnName = "OrderID", SortDirection = ListSortDirection.Descending }); 
          } 
} 
We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S




Marked as answer
Loader.
Up arrow icon