Skip to main content

ColumnView.CustomColumnSort Event

Allows you to implement custom rules according to which a Grid column will sort its data. A column uses these custom rules only when its GridColumn.SortMode property is set to ColumnSortMode.Custom.

Namespace: DevExpress.XtraGrid.Views.Base

Assembly: DevExpress.XtraGrid.v24.1.dll

NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

[DXCategory("Sorting")]
public event CustomColumnSortEventHandler CustomColumnSort

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 therefore no default processing is required.
ListSourceRowIndex1 Gets the index in the data source of the first of the two rows being compared.
ListSourceRowIndex2 Gets the index in the data source of the second of the two rows being compared.
Result Gets or sets the result of a custom comparison.
RowObject1 Gets the first row object being compared.
RowObject2 Gets the second row object being compared.
SortOrder Gets the current sort order applied to the column being processed.
Value1 Gets the first value being compared.
Value2 Gets the second value being compared.

Remarks

Note

This event is not supported in server modes.

The CustomColumnSort event compares cells values in two neighboring rows. These values are stored in the Value1 and Value2 event parameters, the Column parameter returns the column that owns these cells. You can read these parameters to implement any custom comparison logic, and change the Result parameter to specify which of these two rows should go first.

  • set Result to -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.
  • set Result to 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.
  • set Result to 0 to indicate that the rows are equal. In this case the rows will be arranged within the View according to their indexes in the data source.

After you have set the Result parameter to a required value, enable the Handled parameter. Otherwise, if this parameter is left in its default false value, the Grid will invoke the standard sorting mechanism and your custom sort rules will be ignored.

Note

To get row cell values for columns different from what the e.Column parameter returns, read the CustomColumnSortEventArgs.ListSourceRowIndex1 and CustomColumnSortEventArgs.ListSourceRowIndex2 parameters to obtain row indexes in a data source. You can then pass these indexes to the data source API to retrieve rows themselves, as well as row cell values.

Alternatively, you can use the ColumnView.GetListSourceRowCellValue method to get row values.

Online Video

WinForms Grid: Custom Sorting & Non-Sortable Columns.

Example

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) {
        //...
    }
}
See Also