Table of Contents

Sorting

Data Grid allows you to sort data against an unlimited number of columns. A user can sort column data by clicking the column header or using the column header's context menu.

End User Actions

At runtime, a user can click a column header once to sort data in ascending order. A subsequent click reverses the sort order. To clear sorting, hold down the CTRL key and click the column header.

datagrid-sorting

If data is sorted and additional sorting is required on another column, click this column header while holding the SHIFT key down. The control will sort data against the first clicked column, then against the second column, and so on.

To sort data by a specific column, users can also use the column header's context menu. The menu contains the "Sort Ascending" and "Sort Descending" commands. If a column is sorted, the menu contains the "Clear Sorting" command.

datagrid-sorting-contextmenu

You can prevent specific columns from being sorted by users. Use the following properties for this purpose:

  • DataGridControl.AllowSorting — Specifies whether a user can sort by any column.
  • ColumnBase.AllowSorting — Specifies whether a user can sort a specific column.

These properties do not prevent you from sorting data in code.

Sort in Code

You can use the following properties to specify sort settings for columns:

  • ColumnBase.SortDirection — Specifies the sort order. You can set this property to Ascending or Descending to sort the column. When you initialize this property the column is added to the Data Grid's internal sorted column collection. Set the SortDirection property to null to clear sorting for this column, and remove the column from the sorted column collection.

  • ColumnBase.SortIndex — Specifies the zero-based index of the column within the sorted column collection. The Data Grid control sorts data first by the first sorted column, then by the second sorted column, and so on. You can set the SortIndex property to a non-negative value to sort this column in ascending order. Set SortIndex to -1 to clear sorting by this column.

Call the control's inherited DataControlBase.ClearSorting method to remove sorting applied to all columns.

The following code clears sorting and then sorts data by two columns:

dataGrid1.ClearSorting();
gridColumn1.SortDirection = ListSortDirection.Ascending;
gridColumn3.SortDirection = ListSortDirection.Descending;

You can wrap your code with the BeginDataUpdate and EndDataUpdate methods to prevent superfluous updates when changing the control's multiple settings (including sort settings).

dataGrid1.BeginDataUpdate();
dataGrid1.ClearSorting();
gridColumn1.SortIndex = 0;
gridColumn3.SortIndex = 1;
gridColumn2.SortDirection = ListSortDirection.Descending;
dataGrid1.EndDataUpdate();

Sortable Columns and Sort Mode

Data grid can sort columns by edit values, display values, or according to a custom sorting algorithm. Default sort mode is dependent on the column's in-place editor and bound property's data type:

  • Sorting by edit values — All columns, except columns with an embedded ComboBoxEditor.
  • Sorting by display text — Columns with an embedded ComboBoxEditor.
  • No sorting — Columns bound to objects that do not implement the IComparable interface. For instance, image data types do not implement this interface, thus no sorting is available for corresponding columns, by default. See the Custom Sorting section below for information, on how to forcibly sort these columns.

The ColumnBase.SortMode property allows you to change default sort mode for a column. The following options are available:

  • SortMode.Value — Sort by cell edit values.
  • SortMode.DisplayText — Sort by cell display text.
  • SortMode.Custom — Custom sorting. Set the SortMode property to Custom, and then handle the DataGridControl.CustomColumnSort event to implement a custom sorting routine. See Custom Sorting.

Custom Sorting

The CustomColumnSort event allows you to implement custom sorting logic for a specific column. Set the ColumnBase.SortMode property to Custom to enable this event.

If a column's bound data type does not implement the IComparable interface (for instance, an image data type), the Data Grid defaults to preventing sorting for this column. You can, however, enable sorting for this column, as follows:

  • Set the column's AllowSorting property to true (this property's default value is null).
  • Set the column's SortMode property to Custom.
  • Handle the CustomColumnSort event to implement custom sorting.

When you handle the CustomColumnSort event, you should compare two rows specified in the event arguments. Assign the result of the comparison to the Result event parameter as follows:

  • Set Result to -1 if the first row should be displayed above the second row when data is sorted in ascending order.

  • Set Result to 1 if the first row should be displayed below the second row when data is sorted in ascending order.

  • Set Result to 0 if the two rows are equal.

The following example handles the DataGridControl.CustomColumnSort event to sort data in the "FileName" column in a custom manner. The "FileName" column stores file names in the standard "filename.ext" format. The custom sorting routine sorts file names by their extensions.

<mxdg:DataGridControl Name="dataGrid1" CustomColumnSort="DataGrid_CustomColumnSort">
    <mxdg:DataGridControl.Columns>
        <mxdg:GridColumn  Width="*" FieldName="FileName"  SortMode="Custom" />
    </mxdg:DataGridControl.Columns>
</mxdg:DataGridControl>

private void DataGrid_CustomColumnSort(object? sender, DataGridCustomColumnSortEventArgs e)
{
    if (e.Column.FieldName == "FileName")
    {
        string fileName1 = Convert.ToString(e.Value1);
        string fileName2 = Convert.ToString(e.Value2);

        string newfileName1 = extractFileExtension(fileName1) + "." + fileName1;
        string newfileName2 = extractFileExtension(fileName2) + "." + fileName2;
        e.Result = String.Compare(newfileName1, newfileName2);
    }
}

string extractFileExtension(string fileName)
{
    string res = "";
    int dotIndex = fileName.LastIndexOf('.');
    if (dotIndex > 0)
        res = fileName.Substring(dotIndex + 1);
    return res;
}

Respond to Data Sorting

Handle the following events to perform custom actions when data is sorted:

  • DataControlBase.StartSorting — Fires when data is about to be sorted.
  • DataControlBase.EndSorting — Fires when data sorting is complete.