This is a migrated thread and some comments may be shown as answers.

RadDataGrid.Sorting event not working as expected

5 Answers 421 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 28 Feb 2017, 07:42 PM

I want to sort my items via a custom sort order, similar to the implementation detailed here: http://docs.telerik.com/devtools/wpf/controls/radgridview/sorting/custom#custom-sorting-handling-sorting-event

However when I do it seems that e.OldSortingState is always equal to SortingState.None and so I don't get the functionality where first click on column = Ascending, second = Descending, and third = reset to default sorting (ie. None).

The example below should mostly set the sorting to the way it would be if I didn't handle the Sorting event myself. It's only for demonstration. My issue is that I can't ever get to the "else if" and "else" blocks.

private void MainDataGridOnSorting(object sender, GridViewSortingEventArgs e)
 {
     RadGridView radGridView = e.Source as RadGridView;
 
     if (radGridView == null)
     {
         e.Cancel = true;
         return;
     }
 
     DataView dataView = radGridView.DataContext as DataView;
 
     if (dataView == null)
     {
         e.Cancel = true;
         return;
     }
 
     IEnumerable<DataRowView> dataRows = dataView.Cast<DataRowView>();
 
     //If the sorting state is none, sort the items ascending.
     if (e.OldSortingState == SortingState.None)
     {
         e.NewSortingState = SortingState.Ascending;
         e.Column.SortingState = SortingState.Ascending;
 
         dataRows =
             dataRows
                 .OrderBy(row => row.Field<string>(e.Column.UniqueName));
     }
     //If the sorting state is none, sort the items descending.
     else if (e.OldSortingState == SortingState.Ascending)
     {
        // Cannot reach this block
         e.NewSortingState = SortingState.Descending;
         e.Column.SortingState = SortingState.Descending;
 
         dataRows =
             dataRows.OrderByDescending(row => row.Field<string>(e.Column.UniqueName));
     }
     //If the sorting state is descending, apply default sorting to the items.
     else
     {
        // Cannot reach this block
         e.NewSortingState = SortingState.None;
         e.Column.SortingState = SortingState.None;
 
         dataRows =
             dataRows.OrderBy(row => row.Field<int>("PrimaryRow"));
     }
 
     radGridView.ItemsSource = dataRows;
     e.Handled = true;
     e.Cancel = true;
 }

5 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 03 Mar 2017, 01:13 PM
Hello,

I'm attaching a sample project where the code from the article you referenced seems to be working as expected. Could you please have a look at it and let me know if it works for you as well?

If that is the case, please provide more details on your exact setup so that I may further assist you. I will be awaiting your reply.

Regards,
Dilyan Traykov
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Jon
Top achievements
Rank 1
answered on 04 May 2018, 06:45 PM

Hi - The example posted by Dilyan *does* work fine but my real-world app has the same issue as Anthony. e.OldSortingState is *always* None - so the other blocks of code never execute.

I am not able to reproduce this with a simple 'postable' example - but I was able to correct my problem by omitting the cancel property:

 //               e.Cancel = true; // Sort only works correctly with this line omitted.

In fact, Dilyan's example still works fine with this line omitted as well. 

Hope it helps! - Jon

 

0
Dilyan Traykov
Telerik team
answered on 08 May 2018, 09:58 AM
Hello Jon,

I'm glad to hear that you've found a solution to your issue.

Please note, however, that if the Cancel property of the GridViewSortingEventArgs is not set to True, the default sorting logic will be applied to the control. Could you please clarify whether you're setting the column's sorting state yourself as otherwise it's expected for the e.OldSortingState to be always None? Here's the code of interest:

e.NewSortingState = SortingState.Ascending;
e.Column.SortingState = SortingState.Ascending;

The reason no difference in behavior is observed is that in this case, the custom sorting logic is the same as the default behavior when sorting the column. I've updated the project to demonstrate a case where the custom logic first puts the column in the Descending sorting state and then in Ascending, which is the opposite of the default behavior. You can observe that in this case, commenting out the e.Cancel = true line actually changes the behavior of the control.

I hope you find this clarification helpful. If any further questions or concerns arise, please let me know.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Jon
Top achievements
Rank 1
answered on 08 May 2018, 10:18 PM

Dilyan - Yes I was setting the SortingState as you describe but none the less, e.OldSortingState was always None each time the event fired.

However, I believe I found the problem based on your updated example. I was not setting the IsCustomSortingEnabled property to True as you did. When I did this, it fixed the problem. I have added back the 'e.Cancel=true' statement and now e.OldSortingState changes according to the previous value of e.NewSortingState as it should. 

Thanks - Jon

 

0
Dilyan Traykov
Telerik team
answered on 09 May 2018, 11:45 AM
Hello Jon,

Indeed when performing custom sorting, you need to set the IsCustomSortingEnabled property of the column to True so that the default sorting logic is bypassed.

Nonetheless, I'm happy to hear everything is working as expected. Do let me know if I can be of any further assistance.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Anthony
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Jon
Top achievements
Rank 1
Share this question
or