Back to Devexpress

ColumnView.StartSorting Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-8c5bc4ab.md

latest6.2 KB
Original Source

ColumnView.StartSorting Event

Fires before a sorting operation is started.

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 EventHandler StartSorting
vb
<DXCategory("Sorting")>
Public Event StartSorting As EventHandler

Event Data

The StartSorting event's data class is EventArgs.

Remarks

When sorting data by values of a column, the View does the following:

  1. raises the StartSorting event;
  2. sorts data;
  3. raises the ColumnView.EndSorting event.

Note that data grouping requires sorting to be performed. Thus, the StartSorting and ColumnView.EndSorting events are also raised before and after grouping data. Note that you can suppress these events while performing sequential sorting and grouping operations. Use the ColumnView.BeginSort and ColumnView.EndSort methods for this purpose.

Note

The ColumnView.StartSorting and ColumnView.EndSorting events are not raised, when changing the data while the View is already sorted. In this case, the modified row may move to another position due to the current sort rules without firing these events.

See the Sorting in Code document for more information.

Example: Always Sort Data by Values of a Specific Column

This example handles the StartSorting event to sort data in ascending order first by values of the EmployeeID column and then by other columns.

View Example

csharp
public Form1() {
    InitializeComponent();
    gridControl1.DataSource = CreateTable(20);
    gridView1.StartSorting += new EventHandler(gridView1_StartSorting);
}

void gridView1_StartSorting(object sender, EventArgs e) {
    GridView view = sender as GridView;
    GridColumn column = view.Columns["EmployeeID"];
    if (checkEdit1.Checked) {
        if (column.SortOrder != DevExpress.Data.ColumnSortOrder.Ascending || column.SortIndex != view.SortInfo.Count - 1) {
            column.SortIndex = view.SortInfo.Count;
            column.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
        }
    }
    else {
        if (column.SortOrder != DevExpress.Data.ColumnSortOrder.Ascending || column.SortIndex != 0) {
            view.SortInfo.Insert(0, new GridColumnSortInfo(column, DevExpress.Data.ColumnSortOrder.Ascending));
        }
    }
}
vb
Public Sub New()
    InitializeComponent()
    gridControl1.DataSource = CreateTable(20)
    AddHandler gridView1.StartSorting, AddressOf gridView1_StartSorting
End Sub

Private Sub gridView1_StartSorting(ByVal sender As Object, ByVal e As EventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    Dim column As GridColumn = view.Columns("EmployeeID")
    If checkEdit1.Checked Then
        If column.SortOrder <> DevExpress.Data.ColumnSortOrder.Ascending OrElse column.SortIndex <> view.SortInfo.Count - 1 Then
            column.SortIndex = view.SortInfo.Count
            column.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending
        End If
    Else
        If column.SortOrder <> DevExpress.Data.ColumnSortOrder.Ascending OrElse column.SortIndex <> 0 Then
            view.SortInfo.Insert(0, New GridColumnSortInfo(column, DevExpress.Data.ColumnSortOrder.Ascending))
        End If
    End If
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the StartSorting 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.

winforms-grid-always-sort-by-column/CS/Form1.cs#L35

csharp
gridControl1.DataSource = CreateTable(20);
    gridView1.StartSorting += new EventHandler(gridView1_StartSorting);
}

winforms-grid-always-sort-by-column/VB/Form1.vb#L31

vb
gridControl1.DataSource = CreateTable(20)
    AddHandler gridView1.StartSorting, New EventHandler(AddressOf gridView1_StartSorting)
End Sub

See Also

BeginSort()

EndSort()

EndGrouping

EndSorting

StartGrouping

Sort Data in Code

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace