Skip to main content

How to: Implement Custom Sorting

The following example shows how to implement custom sorting with the ColumnView.CustomColumnSort event. In the example, when an ItemFolderDescription column is sorted, the data will be sorted against an IsEmptyRow field instead.

It is assumed that custom sorting is enabled for the ItemFolderDescription column (its GridColumn.SortMode property is set to ColumnSortMode.Custom).

using DevExpress.XtraGrid.Views.Grid;

void gridView1_CustomColumnSort(object sender, 
DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e) {
    GridView view = sender as GridView;
    if(view == null) return;
    try {
        if (e.Column.FieldName == "ItemFolderDescription") {
            object val1 = view.GetListSourceRowCellValue(e.ListSourceRowIndex1, "IsEmptyRow");
            object val2 = view.GetListSourceRowCellValue(e.ListSourceRowIndex2, "IsEmptyRow");
            e.Handled = true;
            e.Result = System.Collections.Comparer.Default.Compare(val1, val2);
        }
    }
    catch (Exception ee) {
        //...
    }
}