Overview
The RadGridView control includes SortDescriptors property at the GridViewTemplate
level which is exposed in RadGridView class for MasterTemplate instance. This collection allows you to
use descriptors which define the sorting property and the sorting direction for the data that is bound to the RadGridView.
As this is a collection, you are able not only to add, but to remove or clear the its entries as well.
When you add a new descriptor to the collection, the data is automatically sorted according to it.
Using SortDescriptor
To enable sorting you need to set the EnableSorting property of the desired template:
Copy[C#] Enable sorting
this.radGridView1.MasterTemplate.EnableSorting = true;
Copy[VB.NET] Enable sorting
Me.RadGridView1.MasterTemplate.EnableSorting = True
Here is how to create and add new SortDescriptor
Copy[C#] Using SortDescriptor
SortDescriptor descriptor = new SortDescriptor();
descriptor.PropertyName = "ShipCountry";
descriptor.Direction = ListSortDirection.Ascending;
this.radGridView1.MasterTemplate.SortDescriptors.Add(descriptor);
Copy[Vb.NET] Using SortDescriptor
Dim descriptor As New SortDescriptor()
descriptor.PropertyName = "ShipCountry"
descriptor.Direction = ListSortDirection.Ascending
Me.RadGridView1.MasterTemplate.SortDescriptors.Add(descriptor)
The PropertyName property defines the property, by which the data will be sorted,
and the SortDirection property allows you to define the sort direction.
Sorting by two or more columns
RadGridView supports sorting by one or more columns. Example of sorting by 2 columns:
Copy[C#] Sorting by two columns
SortDescriptor descriptorShipName = new SortDescriptor();
descriptorShipName.PropertyName = "ShipName";
descriptorShipName.Direction = ListSortDirection.Ascending;
SortDescriptor descriptorFreight = new SortDescriptor();
descriptorFreight.PropertyName = "Freight";
descriptorFreight.Direction = ListSortDirection.Descending;
this.radGridView1.SortDescriptors.Add(descriptorShipName);
this.radGridView1.SortDescriptors.Add(descriptorFreight);
Copy[VB.NET] Sorting by two columns
Dim descriptorShipName As New SortDescriptor()
descriptorShipName.PropertyName = "ShipName"
descriptorShipName.Direction = ListSortDirection.Ascending
Dim descriptorFreight As New SortDescriptor()
descriptorFreight.PropertyName = "Freight"
descriptorFreight.Direction = ListSortDirection.Descending
Me.RadGridView1.SortDescriptors.Add(descriptorShipName)
Me.RadGridView1.SortDescriptors.Add(descriptorFreight)
Note |
|---|
The order of adding the sort expressions to the SortDescriptors collections matters. In the example above, the grid will be first sorted by the ShipName column and then each group will be sorted according to the Freight column. |