GridControl.CustomColumnSort Event

Enables you to sort data using custom rules.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v20.1.dll

Declaration

public event CustomColumnSortEventHandler CustomColumnSort
Public Event CustomColumnSort As CustomColumnSortEventHandler

Event Data

The CustomColumnSort event's data class is CustomColumnSortEventArgs. The following properties provide information specific to this event:

Property Description
Column Gets the column whose values are being compared.
Handled Gets or sets whether a comparison operation is handled and no default processing is required.
ListSourceRowIndex1 Gets the index of the first of the two rows being compared in the data source.
ListSourceRowIndex2 Gets the index of the second of the two rows being compared in the data source.
Result Gets or sets the result of a custom comparison.
SortOrder Gets the sort order applied to the column.
Source Gets the grid control that raised the event.
Value1 Gets the first value being compared.
Value2 Gets the second value being compared.

Remarks

To sort column values using custom logic, set a column's ColumnBase.SortMode property to ColumnSortMode.Custom, and handle the 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.

NOTE

The CustomColumnSort event does not work in Server Mode.

To learn more, see Sorting Modes and Custom Sorting.

Examples

This example demonstrates how to implement custom sorting in the GridControl. Handle the GridControl.CustomColumnSort event, assign your custom sorted list to the e.Result property and set the e.Handled property to true.

<Window x:Class="CustomSorting.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
        Title="Custom Column Sort" Height="300" Width="300" >
    <Grid>
        <dxg:GridControl ItemsSource="{Binding Items}" Grid.Row="1" 
                         CustomColumnSort="OnCustomColumnSort">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="Day" GroupIndex="0" SortMode="Custom" />
                <dxg:GridColumn FieldName="Employee" />
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>
using System.Windows;
using System.Collections.Generic;
using DevExpress.Xpf.Grid;
using CustomSorting;
using System;

namespace CustomSorting {

    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
            DataContext = new SchedulerData();
        }

        void OnCustomColumnSort(object sender, CustomColumnSortEventArgs e) {
            if (e.Column.FieldName == "Day") {
                int dayIndex1 = GetDayIndex((string)e.Value1);
                int dayIndex2 = GetDayIndex((string)e.Value2);
                e.Result = dayIndex1.CompareTo(dayIndex2);
                e.Handled = true;
            }
        }

        int GetDayIndex(string day) {
            return (int)Enum.Parse(typeof(DayOfWeek), day);
        }
    }
}
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; }
    }
}
See Also