Back to Devexpress

Sort Data in Code

windowsforms-692-controls-and-libraries-data-grid-sorting-sorting-in-code.md

latest7.2 KB
Original Source

Sort Data in Code

  • Dec 17, 2024
  • 4 minutes to read

Allow Users to Sort Data

Use the following properties to control whether users can sort data by all columns or only specific columns:

|

Property

|

Description

| | --- | --- | |

View.OptionsCustomization.AllowSort

|

Allows or prevents users from sorting any column in a View.

| |

GridColumn.OptionsColumn.AllowSort

|

Allows or prevents users from sorting by a specific column.

|

csharp
// Prevent users from sorting data by values in the 'OrderId' column
gridView.Columns["OrderId"].OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
vb
' Prevent users from sorting data by values in the 'OrderId' column
gridView.Columns("OrderId").OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False

Note

You can sort data programmatically using the relevant APIs, even if AllowSort properties are disabled.

Sort Data and Change Sort Order

Use the following API to sort data or change the sort order:

|

API

|

Description

| | --- | --- | |

GridColumn.SortOrder

|

Sorts Grid data by the column. Set the SortOrder property to ColumnSortOrder.None to cancel sorting by the column.

| |

GridColumn.SortIndex

|

Specifies the column’s position among sorted columns (in the ColumnView.SortedColumns collection). Set the SortIndex property to -1 to remove sorting by the column.

| |

ColumnView.SortInfo

|

A collection that stores ColumnSortInfo objects for each grid column involved in sorting.

| |

ColumnView.ClearSorting

|

Clears sorting.

|

If you execute multiple sort operations in code, wrap the corresponding code blocks in ColumnView.BeginSort/ColumnView.EndSort method calls to prevent excessive redraw operations.

The following code snippet clears sorting, then sorts data first by the Order Date column in ascending order, followed by the Customer ID column in descending order.

csharp
using DevExpress.Data;

gridView1.BeginSort();
gridView1.SortInfo.ClearAndAddRange(new[] {
  new GridColumnSortInfo(colOrderDate, ColumnSortOrder.Ascending), 
  new GridColumnSortInfo(colCustomerID, ColumnSortOrder.Descending)
});
gridView1.EndSort();

// You can also do the following:
// gridView1.ClearSorting();
// colOrderDate.SortOrder = ColumnSortOrder.Ascending;
// colCustomerID.SortOrder = ColumnSortOrder.Descending;
vb
Imports DevExpress.Data

gridView1.BeginSort()
gridView1.SortInfo.ClearAndAddRange( {
  New GridColumnSortInfo(colOrderDate, ColumnSortOrder.Ascending),
  New GridColumnSortInfo(colCustomerID, ColumnSortOrder.Descending)
})
gridView1.EndSort()

' You can also do the following:
' colOrderDate.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending
' colCustomerID.SortOrder = DevExpress.Data.ColumnSortOrder.Descending

Start and End Sort Events

For each sort operation, the View does the following:

  1. Raises the StartSorting event before the actual sort operation takes place.

  2. Sorts data.

  3. Raises the EndSorting event.

Custom Sorting

Set the column’s SortMode property to ColumnSortMode.Custom. The View raises the CustomColumnSort event before it starts sorting data by values in this column. Handle the CustomColumnSort event to compare data rows and specify the order of rows.

Event parameters include:

e.ResultSet e.Result to 1 if the data row should appear first. Otherwise, set e.Result to -1.e.HandledSet e.Handled to true to cancel the default sorting behavior.

The following code snippet sorts data by values in the “colBirthDate” column in a custom order. It compares dates by months and days, ignoring the year:

csharp
using DevExpress.XtraGrid;

public Form1() {
  colBirthDate.SortMode = ColumnSortMode.Custom;
  colBirthDate.SortIndex = 0;
}
void gridView_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e) {
  if (e.Column.FieldName != "BirthDate") return;
  e.Handled = true;
  int month1 = Convert.ToDateTime(e.Value1).Month;
  int month2 = Convert.ToDateTime(e.Value2).Month;
  if (month1 > month2)
    e.Result = 1;
  else if (month1 < month2)
    e.Result = -1;
  else
  e.Result = System.Collections.Comparer.Default.Compare(Convert.ToDateTime(e.Value1).Day, Convert.ToDateTime(e.Value2).Day);
}
vb
Imports DevExpress.XtraGrid

Public Sub New()
  colBirthDate.SortMode = ColumnSortMode.Custom
  colBirthDate.SortIndex = 0
End Sub
Private Sub gridView_CustomColumnSort(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs)
  If e.Column.FieldName <> "BirthDate" Then
    Return
  End If
  e.Handled = True
  Dim month1 As Integer = Convert.ToDateTime(e.Value1).Month
  Dim month2 As Integer = Convert.ToDateTime(e.Value2).Month
  If month1 > month2 Then
  e.Result = 1
  ElseIf month1 < month2 Then
  e.Result = -1
  Else
  e.Result = System.Collections.Comparer.Default.Compare(Convert.ToDateTime(e.Value1).Day, Convert.ToDateTime(e.Value2).Day)
  End If
End Sub

Tip

The Data Grid can display non-sortable columns (for example, columns with images). Read the following help topic for more information: Custom Sorting and Non-Sortable Columns.

See Also

Sorting

Tutorial: Data Sorting Basics

Tutorial: Sorting by Values or Display Text

Tutorial: Custom Sorting and Non-Sortable Columns

End-User Capabilities: Sorting