Custom Sorting
Custom sorting is a flexible mechanism for sorting RadGridView rows using custom logic. It has a higher priority than the applied SortDescriptors (added either by code or by clicking the header row).
Custom sorting is applied if user sorting is enabled by the EnableSorting or GridViewTemplate.EnableSorting properties. By default, sorting is enabled at all levels.
RadGridView provides two mechanisms for custom sorting:
Handling the CustomSorting event
Replacing the RadGridView sorting mechanism by providing a custom SortComparer
Using the CustomSorting event
Use EnableCustomSorting property to enable the custom sorting functionality.
The CustomSorting event is fired if custom sorting is enabled. It requires the EnableCustomSorting property to be true. The arguments of the event, provide the following properties:
Template: The template that holds the rows that are going to be sorted.
Row1, Row2: The rows to be compared.
SortResult: returns a negative value when
Row1
is beforeRow2
, a positive value ifRow1
is afterRow2
, and zero if the rows have equal values in a specified column.Handled: defines if the comparison of the two rows is processed by the custom algorithm or by the applied sort descriptors.
The following example demonstrates how to handle the CustomSorting event sorting the RadGridView rows ascending by the values of the Freight
column. The defined SortOrder for the Freight
column in this example assumes that row sorting is not applied. All RadGridView rows are processed by the custom logic.
The following example demonstrates the usage of the Handled property of the CustomSorting event arguments. It uses custom sorting to sort the rows ascending by the values of the Freight
column. This sorting is applied to the rows that have a value in the Freight
column greater than "0.33". The rest are handled by the defined SortDescriptor and sorted descending by the values of the Freight
column.
Implementing sorting mechanism by using SortComparer
You can replace the sorting mechanism in RadGridView with a custom one by setting the SortComparer of the GridViewTemplate.
The following example demonstrates how to use a custom sorting mechanism in RadGridView to sort the RadGridView rows ascending by the length of the ShipCity
column:
Create Custom Sort Order Criteria for a Particular Column.
You can use the custom sorting functionality to change the default sorting behavior for a particular column. This will leave the sorting functionality for the other columns intact and the user will be able to sort them in the usual way. However, when the user presses the column header cell for the column that we have changed the sort criteria, it will be sorted by the custom criteria. To achieve this we can use the SortDescriptors collection of RadGridView. For example, you can order the rows by the text length in the Customer column with the following code: