Lesson 6 - Sort Data

By default, data in a GridControl can be sorted by a single column. To apply sorting by the specified column, set the GridColumn.SortOrder property to ColumnSortOrder.Ascending or ColumnSortOrder.Descending.

You can also sort data records by multiple columns. To do this, set the GridControl.SortMode property to GridSortMode.Multiple, and specify GridColumn.SortOrder for each column to be sorted. To specify the sorting sequence applied to columns, assign the GridColumn.SortIndex of these columns.

To prevent end-users from sorting data by values of specific columns, use the GridColumn.AllowSort property.

Open the HelloGrid solution created in Lesson 5 of the current Getting Started tutorial and apply the following sort settings.

  • Sort orders displayed in the grid by the Product.Name column first and then by the Quantity column.
  • Disable sorting by the Shipped column.

You can specify grid sorting settings in XAML...

<dxGrid:GridControl x:Name="grid" ItemsSource="{Binding Orders}" 
                    NewItemRowVisibility="true" 
                    CalculateCustomSummary="OnCalculateCustomSummary" 
                    SortMode="Multiple">
    <dxGrid:GridControl.Columns>
        <dxGrid:TextColumn FieldName="Product.Name" Caption="Product" Width="170" 
                            SortOrder="Descending" SortIndex="0"/>
        <!-- ... -->
        <dxGrid:NumberColumn FieldName="Quantity" 
                             SortOrder="Ascending" SortIndex="1"/>
        <!-- ... -->
        <dxGrid:SwitchColumn FieldName="Shipped" AllowSort="False"/>
    </dxGrid:GridControl.Columns>
</dxGrid:GridControl>

... or C# code.

grid.SortMode = GridSortMode.Multiple;

grid.Columns ["Product.Name"].SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
grid.Columns ["Product.Name"].SortIndex = 0;

grid.Columns ["Quantity"].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
grid.Columns ["Quantity"].SortIndex = 1;

grid.Columns ["Shipped"].AllowSort = DevExpress.Utils.DefaultBoolean.False;

As a result, orders in the grid are sorted by products first and by amount of product units second. End-users can sort data in the grid by tapping a column header or tapping Sort Ascending or Sort Descending in a menu that appears when they touch and hold a column cell or header.

Grid_GettingStarted_Lesson6_Result_iOS

Grid_GettingStarted_Lesson6_Result_Android_Dark

Go on to Lesson 7 of the current Getting Started tutorial to learn how to filter data in the grid.