Back to Devexpress

ColumnView.CustomColumnSort Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-d4d4aed1.md

latest7.8 KB
Original Source

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.v25.2.dll

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

Declaration

csharp
[DXCategory("Sorting")]
public event CustomColumnSortEventHandler CustomColumnSort
vb
<DXCategory("Sorting")>
Public Event CustomColumnSort As CustomColumnSortEventHandler

Event Data

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

PropertyDescription
ColumnGets the column whose values are being compared.
HandledGets or sets whether a comparison operation is handled and therefore no default processing is required.
ListSourceRowIndex1Gets the index in the data source of the first of the two rows being compared.
ListSourceRowIndex2Gets the index in the data source of the second of the two rows being compared.
ResultGets or sets the result of a custom comparison.
RowObject1Gets the first row object being compared.
RowObject2Gets the second row object being compared.
SortOrderGets the current sort order applied to the column being processed.
Value1Gets the first value being compared.
Value2Gets 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).

csharp
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) {
        //...
    }
}
vb
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid

Private Sub GridView1_CustomColumnSort(ByVal sender As System.Object, _
ByVal e As CustomColumnSortEventArgs) Handles GridView1.CustomColumnSort
    Dim view As GridView = sender
    If view Is Nothing Then
        Return
    End If
    Try
        If e.Column.FieldName = "ItemFolderDescription" Then
            Dim val1 As Object = view.GetListSourceRowCellValue(e.ListSourceRowIndex1, "IsEmptyRow")
            Dim val2 As Object = view.GetListSourceRowCellValue(e.ListSourceRowIndex2, "IsEmptyRow")
            e.Handled = True
            e.Result = System.Collections.Comparer.Default.Compare(val1, val2)
        End If
    Catch ee As Exception
    '...
    End Try
End Sub

See Also

SortMode

FieldNameSortGroup

Sort Data in Code

AllowSort

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace