this.datagrid.SortColumnsChanging += Datagrid_SortColumnsChanging;
this.datagrid.SortColumnsChanged += Datagrid_SortColumnsChanged;
private void Datagrid_SortColumnsChanged(object sender, GridSortColumnsChangedEventArgs e)
{
// Here, we have updated the column's items in view based on SortDescriptions.
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
var sortcolumn = this.datagrid.View.SortDescriptions.FirstOrDefault(x => x.PropertyName == e.AddedItems[0].ColumnName);
var sortDescription = new SortDescription(sortcolumn.PropertyName, ListSortDirection.Descending);
this.datagrid.View.SortDescriptions.Remove(sortcolumn);
this.datagrid.View.SortDescriptions.Add(sortDescription);
}
}
private void Datagrid_SortColumnsChanging(object sender, GridSortColumnsChangingEventArgs e)
{
// Initially, we can change the SortDirection of particular column based on columnchanged action.
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
e.AddedItems[0].SortDirection = ListSortDirection.Descending;
}
} |
Hello Srinivasan,
this is a perfect solution! I think it should be added to the official guide: https://help.syncfusion.com/wpf/sfdatagrid/sorting
Is this still the recommended approach? I do think it would make a useful option. Anytime you're dealing with money or percentages/statistics you often want to sort by descending first. It's actually kinda rare to want ascending for numbers. Almost all my apps I do ascending for strings and descending for numeric.
Hi John,
Yes. This is the expected behavior when sorting the columns in SfDataGrid.
However, you can overcome this behavior by using the workaround suggested from our end.
Please let us know if you have any concerns in this.
Regards,
Vijayarasan S