var description = new ColumnSortDescription("LastName", ListSortDirection.Ascending);
DataGrid.SortDescriptions.Add(description);
This topic is designed to quickly familiarize you with sorting behaviors that can be implemented with the XamDataGrid control.
This topic contains the following sections
The following topics are prerequisites to understanding this topic:
The XamDataGrid control allows you to sort records of a Column; the associated content of the remaining columns will be sorted accordingly and then the aggregate of these sorted records is displayed on the UI of the XamDataGrid control. Sorting can be implemented on Local and Remote data sources; for more information, refer to the following topics:
The following steps demonstrate how to implement sorting:
Bind the XamDataGrid control to data.
Creating a new instance of the ColumnSortDescription class, passing as its parameters the PropertyPath
of the Column that you are interested in sorting as well as the desired ListSortDirection, (ascending or descending).
Add the ColumnSortDescription
object to the XamDataGrid control’s ColumnSortDescriptionCollection collection.
A ColumnSortDescription object is used to tell the XamDataGrid control how it should sort its records; each ColumnSortDescription
is responsible for a single sorting behavior, (for example, the LastName Column should be sorted with an Ascending order). Multiple ColumnSortDescription
objects may be assigned to the XamDataGrid control simultaneously to achieve even more complex sorting behaviors, (for more information, see the Example Sorting Records of Multiple Columns:).
The XamDataGrid control determines how it should sort its records by iterating through the ColumnSortDescription
objects, (if any) within its ColumnSortDescriptionCollection collection. If no ColumnSortDescription
objects are present, the XamDataGrid control will persist the original record order of its underlying ItemsSource.
Each ColumnSortDescription
object is assigned a property on the XamDataGrid control’s ItemsSource, (for example, such as a public property associated with a Column) whose records will be sorted, as well as a ColumnSortDescription
, which indicates the sorting direction to be applied to the column records, (either ascending or descending).
The following example demonstrates how to implement a basic sorting behavior. In this scenario, the XamDataGrid control is bound to data containing three Columns : FirstName, LastName, and Territory; with an ascending sort order applied to the LastName Column.
This code snippets creates an instance of the ColumnSortDescription class, with the PropertyPath
of the LastName Column and it uses the ListSortDirection enumeration to pass in an ascending sort order. Then it adds the ColumnSortDescription
object to a ColumnSortDescriptionCollection collection off of the XamDataGrid control, using its SortDescriptions property.
In C#:
var description = new ColumnSortDescription("LastName", ListSortDirection.Ascending);
DataGrid.SortDescriptions.Add(description);
The following screenshot illustrates the resulting sort behavior, note that records within the LastName Column are sorted in an ascending order, with all other associated records being sorted accordingly.
Multiple ColumnSortDescription objects may be applied simultaneously to the XamDataGrid control. In such a circumstance, the XamDataGrid control will attempt to reconcile its sort conditions by performing a sync operation for its assigned ColumnSortDescriptionCollection. This process involves the XamDataGrid control iterating through its ColumnSortDescriptionCollection
collection and will prioritizing the sorting behavior of each ColumnSortDescription
based on the order that it is present in the collection, (item[0] executed first, item[1] executed second, etc). In this way, multi-column sorting is possible, wherein data may be sorted into groups, which are also sorted.
This multi-sort behavior is demonstrated in the following example.
In this scenario, two ColumnSortDescription
objects have been applied to the XamDataGrid control; the primary ColumnSortDescription
targets the Territory Column and sorts in an ascending order, the second targets the LastName Column
and also sorts in an ascending order. Using this combination of ColumnSortDescription
objects, a pseudo grouping behavior is achieved, where the names of individuals assigned to a given territory are grouped by that territory, (the list of which is sorted in an ascending order) and each group of individuals is also listed in an ascending order.
In C#:
var sorting1 = new ColumnSortDescription("Territory", ListSortDirection.Ascending);
var sorting2 = new ColumnSortDescription("LastName", ListSortDirection.Ascending);
DataGrid.SortDescriptions.Add(sorting1);
DataGrid.SortDescriptions.Add(sorting2);
The following screenshot illustrates the resulting sort behavior on the Territory and LastName Columns.
Note that the Territory Column has been sorted first, in an ascending order followed by the LastName column which is also sorted in an ascending order. The XamDataGrid control has synced these sorting operations, so that both conditions are satisfied, (with the order that the ColumnSortDescription are executed in indicating their priority). In this way, all data items assigned to a territory are grouped by that territory and each group of item is also sorted in an ascending order.
The following table lists topics that are related to this topic: