Step 2: Enable Sort Operations
- 2 minutes to read
In This Article
This step describes how to allow users to sort rows:
- Implement sort operations in the virtual source.
- Enable these operations in the GridControl.
#Sort Orders
The Issues Service allows you to apply the following sort orders:
- Default - records are shown from newest to oldest
- Created Date in Descending order
- Votes in any order
public enum IssueSortOrder {
Default,
CreatedDescending,
VotesAscending,
VotesDescending
}
The Step 1: Fetch Data topic demonstrates how to fetch rows with the default sort order:
public MainPage() {
// ...
source.FetchRows += (o, e) => {
e.Result = FetchRowsAsync(e);
};
}
static async Task<FetchRowsResult> FetchRowsAsync(FetchRowsAsyncEventArgs e) {
var take = 30;
var issues = await IssuesService.GetIssuesAsync(
skip: e.Skip,
take: take,
sortOrder: IssueSortOrder.Default,
filter: null);
return new FetchRowsResult(issues, hasMoreRows: issues.Length == take);
}
#Allow Users to Sort Rows
Implement sort operations in the virtual source:
Use the FetchEventArgsBase.SortOrder property to get the GridControl‘s sort options.
Parse these sort options and return a sort order. The InfiniteAsyncSource.FetchRows event handler uses this sort order when the GridControl fetches rows.
static IssueSortOrder GetIssueSortOrder(SortDefinition[] sortOrder) { if (sortOrder.Length > 0) { var sort = sortOrder.Single(); if (sort.PropertyName == "Created") { if (sort.Direction != ListSortDirection.Descending) throw new InvalidOperationException(); return IssueSortOrder.CreatedDescending; } if (sort.PropertyName == "Votes") { return sort.Direction == ListSortDirection.Ascending ? IssueSortOrder.VotesAscending : IssueSortOrder.VotesDescending; } } return IssueSortOrder.Default; } static async Task<FetchRowsResult> FetchRowsAsync(FetchRowsAsyncEventArgs e) { IssueSortOrder sortOrder = GetIssueSortOrder(e.SortOrder); var take = 30; var issues = await IssuesService.GetIssuesAsync( skip: e.Skip, take: take, sortOrder: sortOrder, filter: null); return new FetchRowsResult(issues, hasMoreRows: issues.Length == take); }
Allow users to sort GridControl rows by the Votes and Created columns. Set the ColumnBase.AllowSorting property to true for these columns:
<dxg:GridDateColumn FieldName="Created" AllowSorting="True"/> <dxg:GridTextColumn FieldName="Votes" AllowSorting="True"/>
#Continue or Review
- Proceed to Step 3 - Enable Filter Operations.
- Review Step 1 - Fetch Data.