RadGrid for ASP.NET

Sorting expressions Send comments on this topic.
Sorting > Sorting expressions

Glossary Item Box

You can programmatically manipulate GridTableView.SortExpressions collection to sort the grid items by one or more data-fields (used as a sorting criteria).

When you click on a column, Telerik RadGrid will add the GridColumn.SortExpression string to the corresponding GridTableView.SortExpressions collection. Removing the sorting will remove an item from that collection.

Sorting with declarative sort expressions

You can set default sort expression for a column in the MasterTableView/GridTableView directly in the html code of the page:

ASPX/ASCX Copy Code
<MasterTableView>
...
   
<SortExpressions>
       
<rad:GridSortExpression FieldName="CompanyName" SortOrder="Ascending"></rad:GridSortExpression>
   
</SortExpressions>
...
  
<DetailTables>
      
<GridTableView>
...
        
<SortExpressions>
           
<rad:GridSortExpression FieldName="OrderDate" SortOrder="Descending"></rad:GridSortExpression>
        
</SortExpressions>
...
     
<GridTableView>
   
</DetailTables>
</
MasterTableView>
Thus the respective column will be sorted initially in the order specified by the SortOrder argument.
 

Sorting Programmatically

Grid sort can be manipulated programmatically through GridSortExpression class. An instance of this class represents a single 'column' sort.
You should use code similar to the following :

 

C# Copy Code
GridSortExpression expression = new GridSortExpression();
expression.FieldName =
"CompanyName";
expression.SortOrder = GridSortOrder.Descending;
this.RadGrid1.MasterTableView.SortExpressions.AddSortExpression( expression );

this.RadGrid1.MasterTableView.Rebind();

VB.NET Copy Code
Dim expression as GridSortExpression = new GridSortExpression()
expression.FieldName = "CompanyName"
expression.SortOrder = GridSortOrder.Descending
Me.RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression)

Me.RadGrid1.MasterTableView.Rebind()

SortExpressions items are preserved in the view state for a GridTableView in each hierarchy level.

Expression string should be a data-field name - the same as the one used in DataView.Sort expression. Its FieldName property should contain only a single data-field name.

Sorting is done internally by a DataView. You should be sure that you have specified Expression property of GridSortExpression class the right way or an exception will be thrown when the grid is databound.

When using the Sort property of GridColumn objects you can also specify the button type of the button that would appear in GridTableView's header. You should assign a value of GridColumn.HeaderButtonType property according to one of the values in GridHeaderButtonType enumeration.