/winforms-grid-always-sort-by-column

Always sort data by values of a specific column in the WinForms Data Grid.

Primary LanguageC#OtherNOASSERTION

WinForms Data Grid - 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:

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));
        }
    }
}

Files to Reivew

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)