Edit this page

Setting Sorting Programmatically

Sorting can be performed programmatically by adding descriptors to the RadGridView.SortDescriptors collection.

Overview

RadGridView includes SortDescriptors property at the GridViewTemplate level which is exposed in the 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 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.

Enable Sorting

this.radGridView1.MasterTemplate.EnableSorting = true;

Me.RadGridView1.MasterTemplate.EnableSorting = True

Here is how to create and add new SortDescriptor.

Using SortDescriptor


SortDescriptor descriptor = new SortDescriptor();
descriptor.PropertyName = "ShipCountry";
descriptor.Direction = ListSortDirection.Ascending;
this.radGridView1.MasterTemplate.SortDescriptors.Add(descriptor);

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. The bellow example shows how you can sort by 2 columns.

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);

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)

Figure 1: Sorting by Two Columns

gridview-sorting-setting-sorting-programmatically 001

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.

See Also