Sorting Modes and Custom Sorting

Overview

The Grid Control supports data sorting by cell values and display text. For each column, you can specify how data should be sorted - by the edit or display values, or using custom logic. To specify the required sort mode, use the ColumnBase.SortMode property.

Custom Sorting

To sort column values using custom logic, set a column's ColumnBase.SortMode property to ColumnSortMode.Custom, and handle the GridControl.CustomColumnSort event.

When this event is fired, two rows should be compared. The column being processed is specified by the Column parameter. The Value1 and Value2 parameters identify the values of the rows within this column. The result of the custom comparison should be set to the Result parameter as follows:

  • -1 if the first row should be positioned above the second row when data is sorted in ascending order. When data is sorted in descending order, the first row will be positioned below the second row.
  • 1 if the first row should be positioned below the second row when data is sorted in ascending order. When data is sorted in descending order the first row will be positioned above the second row.
  • 0 to indicate that the rows are equal. In this case, the rows will be arranged within a grid according to their indices in the data source.

The event parameter's Handled property should be set to true if the comparison operation was handled. Leave this parameter set to false to invoke the default comparison mechanism after your event handler has finished. In this instance, the result of the custom comparison operation is ignored.

Example: How to Implement Custom Sorting

This example demonstrates how to implement custom sorting in the Grid Control. To do this, handle the GridControl.CustomColumnSort event, assign your custom sorted list to the e.Result property and set the e.Handled property to True.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CustomSorting"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Grid="using:DevExpress.UI.Xaml.Grid"
    x:Class="CustomSorting.MainPage"
    mc:Ignorable="d">

    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <CheckBox Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Custom Sort" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
        <Grid:GridControl Grid.Row="1" Name="grid" AutoGenerateColumns="True" NavigationStyle="None"
                          CustomColumnSort="grid_CustomColumnSort" />
    </Grid>
</Page>