Skip to main content

Sorting Modes and Custom Sorting

  • 4 minutes to read

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>
using System.Collections.Generic;
using System.Collections.ObjectModel;
using DevExpress.XtraGrid;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace CustomSorting {
    public sealed partial class MainPage : Page {
        public MainPage() {
            this.InitializeComponent();
            grid.ItemsSource = new MonthList();
            grid.SortBy(grid.Columns[0]);
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e) {
            grid.Columns[0].SortMode = ColumnSortMode.Custom;
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e) {
            grid.Columns[0].SortMode = ColumnSortMode.Default;
        }

        private void grid_CustomColumnSort(object sender, DevExpress.UI.Xaml.Grid.CustomColumnSortEventArgs e) {
            e.Result = Comparer<int>.Default.Compare(e.ListSourceRowIndex1, e.ListSourceRowIndex2);

            e.Handled = true;
        }
    }
    public class Month {
        public string month { get; set; }
    }
    public class MonthList : ObservableCollection<Month> {
        public MonthList()
            : base() {
            Add(new Month() { month = "January" });
            Add(new Month() { month = "February" });
            Add(new Month() { month = "March" });
            Add(new Month() { month = "April" });
            Add(new Month() { month = "May" });
            Add(new Month() { month = "June" });
            Add(new Month() { month = "July" });
            Add(new Month() { month = "August" });
            Add(new Month() { month = "September" });
            Add(new Month() { month = "October" });
            Add(new Month() { month = "November" });
            Add(new Month() { month = "December" });
        }
    }
}