|
| 1 | +--- |
| 2 | +title: Sorting and Grouping in Descending Order in RadDataGrid |
| 3 | +description: Learn how to sort and group items in the RadDataGrid for UI for .NET MAUI in descending order. |
| 4 | +type: how-to |
| 5 | +page_title: Setting Sort and Group Descriptors to Descending Order in RadDataGrid |
| 6 | +meta_title: Setting Sort and Group Descriptors to Descending Order in RadDataGrid |
| 7 | +slug: datagrid-sorting-grouping-descending-order |
| 8 | +tags: datagrid, ui-for-dotnet-maui, sort-descriptors, group-descriptors, descending-order |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 11.1.0 | DataGrid for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I need when the user groups the DataGrid through the U, the groups to appear in descending order. Currently, the items are sorted by `FirstContactDate` in descending order, but when grouped, the grouping defaults to ascending order. The goal is to allow users to see the latest entries grouped by `FirstContactDate` in descending order. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to sort and group items in descending order in DataGrid? |
| 24 | +- How to set group descriptors to descending order in Telerik DataGrid for .NET MAUI? |
| 25 | +- How to change group descriptor order dynamically during grouping? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To achieve sorting and grouping in descending order, follow one of the approaches below: |
| 30 | + |
| 31 | +### Option 1: Set Sort and Group Descriptors Explicitly |
| 32 | + |
| 33 | +Define both `SortDescriptors` and `GroupDescriptors` with the desired `SortOrder` in the XAML configuration: |
| 34 | + |
| 35 | +```xaml |
| 36 | +<telerik:RadDataGrid.GroupDescriptors> |
| 37 | + <telerik:PropertyGroupDescriptor PropertyName="FirstContactDate" SortOrder="Descending" /> |
| 38 | +</telerik:RadDataGrid.GroupDescriptors> |
| 39 | + |
| 40 | +<telerik:RadDataGrid.SortDescriptors> |
| 41 | + <telerik:PropertySortDescriptor PropertyName="FirstContactDate" SortOrder="Descending" /> |
| 42 | +</telerik:RadDataGrid.SortDescriptors> |
| 43 | +``` |
| 44 | + |
| 45 | +### Option 2: Change Group Sort Order at Runtime |
| 46 | + |
| 47 | +Use the `GroupDescriptors.CollectionChanged` event to dynamically update the `SortOrder` of the group descriptor when a column is grouped. Example: |
| 48 | + |
| 49 | +```csharp |
| 50 | +public partial class MainPage : ContentPage |
| 51 | +{ |
| 52 | + public MainPage() |
| 53 | + { |
| 54 | + InitializeComponent(); |
| 55 | + BindingContext = new RegistrationViewModel(); |
| 56 | + |
| 57 | + this.dataGrid.GroupDescriptors.CollectionChanged += GroupDescriptors_CollectionChanged; |
| 58 | + } |
| 59 | + |
| 60 | + private void GroupDescriptors_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
| 61 | + { |
| 62 | + if (e.Action != NotifyCollectionChangedAction.Add) |
| 63 | + return; |
| 64 | + |
| 65 | + var groupDescriptor = e.NewItems[0] as GroupDescriptorBase; |
| 66 | + if (groupDescriptor != null) |
| 67 | + { |
| 68 | + groupDescriptor.SortOrder = SortOrder.Descending; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## See Also |
| 75 | + |
| 76 | +- [DataGrid Documentation](https://docs.telerik.com/devtools/maui/controls/datagrid/overview) |
0 commit comments