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

Default SortDirection

When I click on a column to sort, the default SortDirection is Ascending.
I would like that the first time I click on any column, the default SortDirection is Descending.

Is it possible to achieve that?

5 Replies

SV Srinivasan Vasu Syncfusion Team August 2, 2017 01:45 PM UTC

Hi Luca, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and we have prepared a sample as per your requirement. You can achieve your requirement by using SfDataGrid.SortColumnsChanging and SfDataGrid.SortColumnsChanged event. 
 
Please refer the below code example. 
 
        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; 
            } 
        }              
 
 
 
 
Regards, 
Srinivasan 
 



LS Luca Spolidoro August 7, 2017 05:48 PM UTC

Hello Srinivasan,

this is a perfect solution! I think it should be added to the official guide: https://help.syncfusion.com/wpf/sfdatagrid/sorting



BR Balamurugan Rajaraman Syncfusion Team August 8, 2017 12:11 PM UTC

Hi Luca, 
 
Thanks for the update. 
 
We will consider your suggestion and include this in to our documentation. 
 
Regards, 
Balamurugan R 



JO John June 19, 2022 04:30 AM UTC

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.



VS Vijayarasan Sivanandham Syncfusion Team June 20, 2022 02:27 PM UTC

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


Loader.
Live Chat Icon For mobile
Up arrow icon