How set default sort for column in DataGrid?

2 Answers 38 Views
DataGrid
Dmitry
Top achievements
Rank 1
Iron
Dmitry asked on 16 Feb 2022, 04:40 AM | edited on 16 Feb 2022, 05:15 AM
I have DataGrid template column. As SortDescriptor i'm use DelegateSortDescriptor. Seted SortOrder="Descending". When i start app i want that this column was sorted descending. Now when app started column is not sorted. When i click on header then column sorted properly.
...
                    <telerikDataGrid:DataGridTemplateColumn HeaderText="Name" HeaderStyle="{StaticResource DataGridHeaderStyle}"
                                                            CanUserSort="True"
                                                        >
                        <telerikDataGrid:DataGridTemplateColumn.SortDescriptor>
                            <telerikCommon:DelegateSortDescriptor KeyLookup="{StaticResource FileInfoExKeyLookup}" SortOrder="Descending" />
                        </telerikDataGrid:DataGridTemplateColumn.SortDescriptor>
...
                        <telerikDataGrid:DataGridColumn.CellContentTemplate>
                            <DataTemplate>
                                <HorizontalStackLayout Margin="5,5">
                                    <Label Text="{Binding Path=IconInfo.Code}" Style="{StaticResource FontAwesome}" FontSize="Medium" TextColor="{Binding Path=IconInfo.Color}"
                                            HorizontalOptions="Center" VerticalOptions="Center"/>
                                    <Label Text="{Binding ESFileInfo.fileName}" Style="{StaticResource PrimaryLabel}" Margin="5"
                                       HorizontalOptions="Start" VerticalOptions="Center"
                                       VerticalTextAlignment="Center"
                                       />
                                </HorizontalStackLayout>
                            </DataTemplate>
                        </telerikDataGrid:DataGridColumn.CellContentTemplate>

2 Answers, 1 is accepted

Sort by
0
Didi
Telerik team
answered on 16 Feb 2022, 01:12 PM | edited on 16 Feb 2022, 02:25 PM

Hi Dmitry,

The DataGrid can be sorted through the UI or programmatically. When tapping on the column header, you can sort the DataGrid through the UI.

In order to sort the DataGrid programmatically, you need to add the descriptors in code after setting the DataGird ItemsSource

this.dataGrid.SortDescriptors.Add(new PropertySortDescriptor() { PropertyName = "Name", SortOrder = Telerik.XamarinForms.Common.SortOrder.Ascending});

Could you please share what will be the target operating system of the application?  Do you prefer specific operating system or the app will target Android, iOS, macCatalyst and Windows? 

I look forward to your reply.

Regards,
Didi
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Dmitry
Top achievements
Rank 1
Iron
commented on 17 Feb 2022, 01:23 AM

Hi Didi. I know how sort programmatically and UI. I want set property in XAML. E.g. when SortOrder=Descending then column has sorted after startup.

Target OS for application Android and Windows.
0
Didi
Telerik team
answered on 17 Feb 2022, 04:36 PM | edited on 17 Feb 2022, 04:43 PM

Hello Dmitry,

We have tested the SortDescriptor property and actually when it is attached to a TemplateColumn, the Column is not initially sorted. If you tap on the column header, then you can sort the column. 

I have logged the initially sorting behavior on your behalf in our feedback portal. Follow the item at this link: https://feedback.telerik.com/maui/1554359-datagrid-templatecolumn-is-not-initially-sorted-when-sortdescriptor-is-applied Find your Telerik points updated as a small sign for bringing this to our attention.

Solution:

In order to sort the template column initially:

1. add the SortDescriptor to the Template column in XAML:

  <telerikDataGrid:DataGridTemplateColumn.SortDescriptor>
                <telerikCommon:PropertySortDescriptor PropertyName="Age" />
</telerikDataGrid:DataGridTemplateColumn.SortDescriptor>

2. then execute the ColumnHeaderTapCommand on the TemplateColumn in code behind, inside the Page's constructor, after setting the DataGrid ItemsSource:

this.dataGrid.CommandService.ExecuteCommand(DataGridCommandId.ColumnHeaderTap, this.dataGrid.Columns[1]);

The ExecuteCommand receives two parameters, the first is the DataGridCommand id and for the second parameter - the column index where the command will be executed. In my case the template column is on index 1. 

Regards,
Didi
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DataGrid
Asked by
Dmitry
Top achievements
Rank 1
Iron
Answers by
Didi
Telerik team
Share this question
or