If you want to set an initial sorting on a column on a Telerik RadGrid, use the “NeedDataSource” event and add a GridSortExpression to the MasterTableView.SortExpressions
protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) { // Set datasource here… if (!IsPostBack) {
// Clear the current sortexpressions this.RadGrid.MasterTableView.SortExpressions.Clear(); // Create "Date" sorting GridSortExpression expression = new GridSortExpression(); expression.FieldName = "Date"; expression.SortOrder = GridSortOrder.Descending; // Set initial sortexpression to the [Date] column this.opdrachtenRadGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
} }
In the aspx page:
<telerik:GridDateTimeColumn DataField="Date" HeaderText="Date" SortExpression="Date" UniqueName="Date" FilterControlWidth="70px" ShowFilterIcon="false" DataType="System.DateTime" CurrentFilterFunction="EqualTo" PickerType="DatePicker" AutoPostBackOnFilter="true" HtmlEncode="false" DataFormatString="{0:dd-MM-yyyy}" meta:resourcekey="Date"> <ItemStyle Width="70px" /> </telerik:GridDateTimeColumn>
Or you could use the “SortExpressions” tag in the MasterTableView tag:
<telerik:RadGrid ID="RadGrid1" runat="server"> <MasterTableView> <SortExpressions> <telerik:GridSortExpression FieldName="Name" SortOrder="Ascending" /> </SortExpressions> <Columns> <telerik:GridBoundColumn AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" DataField="Name" FilterControlWidth="200px" meta:resourcekey="CustomerNameColumn" UniqueName="Name" SortExpression="Name" ShowFilterIcon="true"> <ItemStyle Width="200px" /> </telerik:GridBoundColumn>
……
Thanks, this has been useful.