wpf-120196-controls-and-libraries-data-grid-bind-to-data-bind-to-any-data-source-with-virtual-sources-use-virtual-sources-step-2-enable-sort-operations.md
You can allow users to sort rows in the GridControl. Complete the following steps:
Explore the full source code in the following examples and demos:
Infinite Scrolling SourceView Example Run DemoPaged SourceView Example Run Demo
The Issues Service allows you to apply the following sort orders:
public enum IssueSortOrder {
Default,
CreatedDescending,
VotesAscending,
VotesDescending
}
The code snippet below implements the default sort order in the GridControl:
[Command]
public void FetchIssues(FetchRowsAsyncArgs args) {
args.Result = GetIssuesAsync(args);
}
async Task<FetchRowsResult> GetIssuesAsync(FetchRowsAsyncArgs args) {
var take = args.Take ?? 30;
var issues = await IssuesService.GetIssuesAsync(
skip: args.Skip,
take: take,
sortOrder: GetIssueSortOrder(args.SortOrder),
filter: null
);
return new FetchRowsResult(issues, hasMoreRows: issues.Length == take);
}
static IssueSortOrder GetIssueSortOrder(SortDefinition[] sortOrder) {
return IssueSortOrder.Default;
}
SortDefinition class instance to get GridControl sort options.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;
}
Assign true to the ColumnBase.AllowSorting property.
Set the ColumnBase.DefaultSortOrder property to Descending to specify the default sort order. This order is applied when a user clicks the column’s header for the first time.
<dxg:GridColumn FieldName="Votes"
AllowSorting="True"
DefaultSortOrder="Descending"/>
Assign true to the ColumnBase.AllowSorting property.
The Issues Service can sort rows by the Created Date field in descending order only. Set the ColumnBase.AllowedSortOrders property to Descending to disable other sort orders by this column from the UI.
<dxg:GridColumn FieldName="Created"
AllowSorting="True"
AllowedSortOrders="Descending"/>
In this tutorial, the Issues Service can sort data by a single column only. If your data source can sort data by multiple columns, set the GridViewBase.AllowGroupingSortingBySingleColumnOnly property to False. In this case, the GridControl can be sorted by multiple columns. Users should hold the Shift key and click the required column headers to sort data against these columns.
The Created Date column allows users to sort data in descending order only. When a user clicks the column header, the GridControl does not change the sort order. Users should hold the Ctrl key and click the column header to clear the column’s sort order.
Set the DataViewBase.ColumnSortClearMode property to Click. This allows users to apply or clear the sort order by clicking a column header:
<dxg:GridControl.View>
<dxg:TableView ColumnSortClearMode="Click"/>
</dxg:GridControl.View>