Skip to main content
A newer version of this page is available. .

Sorting Modes and Custom Sorting

  • 5 minutes to read

Note

Sorting by display text and custom sorting are not supported in Server Mode.

Overview

The DXGrid supports data sorting by cell values and display text. For each column, you can specify how their 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 View according to their indices in a data source.

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

Sorting by another column

GridControl’s default sorting and grouping behavior is as follows. On clicking a column header or changing the ColumnBase.SortOrder property, data is sorted by the corresponding field. On grouping against a column, data groups are created based on the values of this column.

The ColumnBase.SortFieldName property allows you to change this behavior. When sorting/grouping is applied to a specific column, you can sort/group data against another field instead. To do this, set the ColumnBase.SortFieldName property to the required field name.

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.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomSorting {
    public class SchedulerData {
        const int daysInWeek = 7;
        string[] employees = new string[] { "Jane", "Martin", "John", "Jack", "Amanda", "Carmen", "Wins", "Todd", "Ashley" };
        Random rnd = new Random();
        public List<SchedulerItem> Items { get; set; }
        public SchedulerData() {
            Items = new List<SchedulerItem>();
            GenerateRandomData();
        }
        void GenerateRandomData() {
            for (int i = 0; i < daysInWeek; i++) {
                int e1 = rnd.Next(employees.Length - 1);
                int e2 = e1 + 1;
                string day = DateTime.Today.AddDays(i).DayOfWeek.ToString();
                Items.Add(new SchedulerItem() { Day = day, Employee = employees[e1] });
                Items.Add(new SchedulerItem() { Day = day, Employee = employees[e2] });
            }
        }
    }

    public class SchedulerItem {
        public string Day { get; set; }
        public string Employee { get; set; }
    }
}