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

ASPxGridView.CustomColumnSort Event

Enables you to sort data using custom rules.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v20.2.dll

NuGet Package: DevExpress.Web

Declaration

public event ASPxGridViewCustomColumnSortEventHandler 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. Inherited from GridCustomColumnSortEventArgs.
ListSourceRowIndex1 Gets the index in the data source of the first of the two data items (row, card or record) being compared. Inherited from GridCustomColumnSortEventArgs.
ListSourceRowIndex2 Gets the index in the data source of the second of the two data items (row, card or record) being compared. Inherited from GridCustomColumnSortEventArgs.
Result Gets or sets the result of a custom comparison. Inherited from GridCustomColumnSortEventArgs.
SortOrder Gets the sort order applied to the column (row for ASPxVerticalGrid) being processed. Inherited from GridCustomColumnSortEventArgs.
Value1 Gets the first value being compared. Inherited from GridCustomColumnSortEventArgs.
Value2 Gets the second value being compared. Inherited from GridCustomColumnSortEventArgs.

The event data class exposes the following methods:

Method Description
GetRow1Value(String) Returns the specified column’s value in the first data item (row, card or record) being compared. Inherited from GridCustomColumnSortEventArgs.
GetRow2Value(String) Returns the specified column’s value in the second data item (row, card or record) being compared. Inherited from GridCustomColumnSortEventArgs.

Remarks

A column’s Settings.SortMode (GridDataColumnSettings.SortMode) property specifies how the grid’s data is sorted when sorting is applied to this column. If this property is set to ‘Custom’, a custom sorting algorithm can be implemented for this column by handling the CustomColumnSort event.

When the CustomColumnSort event is fired, two rows should be compared. The column being processed is specified by the CustomColumnSortEventArgs.Column parameter. The GridCustomColumnSortEventArgs.Value1 and GridCustomColumnSortEventArgs.Value2 parameters identify the row values within this column.

The result of the custom comparison should be set to the GridCustomColumnSortEventArgs.Result parameter as shown below:

  • 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 grid according to their indexes in the data source.

The GridCustomColumnSortEventArgs.Handled parameter should be set to true if the current 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 case, the custom comparison operation’s result is ignored.

Example

The example below sorts the Country column by character length in descending order.

<dx:ASPxGridView ID="ASPxGridView1" runat="server"  OnCustomColumnSort="grid_CustomColumnSort">
    <Columns>
        <dx:GridViewDataColumn FieldName="Country" SortOrder="Descending">
                <Settings SortMode="Custom" />
        </dx:GridViewDataColumn>
        ...
    </Columns>
  ...
</dx:ASPxGridView>
protected void grid_CustomColumnSort
(object sender, DevExpress.Web.CustomColumnSortEventArgs e) {
    if (e.Column.FieldName == "Country") {
        e.Handled = true;
        string s1 = e.Value1.ToString(), s2 = e.Value2.ToString();
        if (s1.Length > s2.Length)
            e.Result = 1;
        else
            if (s1.Length == s2.Length)
                e.Result = Comparer.Default.Compare(s1, s2);
            else
                e.Result = -1;
    }
}

Result:

CustomColumnSort

See Also