ColumnView.CustomColumnSort Event
Provides the ability to sort data using custom rules.
Namespace: DevExpress.XtraGrid.Views.Base
Assembly: DevExpress.XtraGrid.v18.1.dll
Declaration
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 sever modes.
A column’s GridColumn.SortMode property specifies how the View’s data is sorted when sorting is applied to this column. If this property is set to ColumnSortMode.Custom, a custom sorting algorithm can be implemented for this column via the CustomColumnSort event.
When the CustomColumnSort 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:
- 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.
The 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 the latter case the custom comparison operation’s result is ignored.
Note
The values being compared are specified by the CustomColumnSortEventArgs.Value1 and CustomColumnSortEventArgs.Value2 parameters. To get row values in other columns while handling the CustomColumnSort event use the methods provided by your data source. See the CustomColumnSortEventArgs.ListSourceRowIndex1 and CustomColumnSortEventArgs.ListSourceRowIndex2 parameters to obtain the indexes of the rows in the data source. Then use the methods of your data source to get the rows themselves and row 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) {
//...
}
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomColumnSort event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.