Sort Vertical Grid Data
- 4 minutes to read
A user can sort data in a VGridControl from a row header’s context menu. Right-click a row header and select Sort Ascending or Sort Descending to change the sort order. To make all rows display data in the same order as the data source, select Clear Sorting.
Set the AllowSort property to false to disable this functionality.
Sort Data with a Click on a Row Header
You can enable the control’s AllowSortOnClick option to sort data when users click on a row header. Consecutive clicks change the sort order. Ctrl+Click clears sorting. If the AllowSort option is disabled, this property is not in effect.
If the control’s AllowSortOnClick option is enabled, you can use a row’s AllowSortOnClick property to disable this functionality for the row.
Example
The code below allows users to click a row header to sort data in all rows except the ID row.
vGridControl.OptionsBehavior.AllowSort = true;
vGridControl.OptionsBehavior.AllowSortOnClick = true;
erID.Properties.AllowSortOnClick = DefaultBoolean.False;
Sort Data in Code
Use a row’s SortOrder property to specify whether the row is sorted in ascending or descending order. If this property is set to None, data is not sorted. To make all rows display data in the same order as the data source, call the ClearSorting() method.
Sort by Multiple Rows
Data in the control can be sorted by multiple rows. For example, in a list of employees, you can first sort employees by surname and then by name. The resulting order of records depends on which list is sorted first. Users can hold Shift and click headers to sort by multiple rows.
A row’s SortIndex property specifies the index of the row in the collection of rows by which data is sorted. If you assign a non-negative value to this property, the control inserts the row to the collection of sorted rows at the specified position and sorts data in the row in ascending order.
When a user sorts data or you change a row’s SortOrder or SortIndex property value, the control raises the StartSorting event. When data is sorted, the EndSorting event fires.
Prevent Multiple Updates
Each time the sort order changes, the control reloads data from the data source. If you sort data by multiple rows, use the BeginSort() and EndSort() methods to prevent multiple updates. These methods suppress the StartSorting and EndSorting events.
Example
The code below shows how to sort data in code. If you sort data by multiple rows, call the BeginSort() and EndSort() methods to prevent multiple updates.
vGridControl.BeginSort();
erModelPrice.Properties.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
erDiscount.Properties.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
vGridControl.EndSort();
Localize Context Menu Commands
You can use localizer objects to change captions of the control’s context menu commands. To do this, create a VGridLocalizer descendant, and override its GetLocalizedString method.
You can also use satellite assemblies to localize the control. See the following topic for more information: Localization.
Example
The code below demonstrates how you can localize Vertical Grid‘s captions in the row header context menu.
using DevExpress.XtraVerticalGrid.Localization;
VGridLocalizer.Active = new MyVGridLocalizer();
class MyVGridLocalizer : VGridLocalizer {
public override string Language { get { return "English"; } }
public override string GetLocalizedString(VGridStringId id) {
switch (id) {
case VGridStringId.MenuPropertySortAscending: return "Sort Ascending";
case VGridStringId.MenuPropertySortDescending: return "Sort Descending";
case VGridStringId.MenuPropertyClearSorting: return "Clear Sorting";
case VGridStringId.MenuPropertyClearSortingAllProperties: return "Clear All Sorting";
default: return base.GetLocalizedString(id);
}
}
}