This is a migrated thread and some comments may be shown as answers.

Default sort order?

4 Answers 385 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sergey
Top achievements
Rank 2
Sergey asked on 22 Sep 2010, 02:13 PM
Hey, 

Trying to make a grid sort a column in descending order after clicking once on the header instead of the default ascending first. Essentially, the behaviour of this demo. From this documentation I get the impression that I can just do this from markup, as the demo does it by creating a SortExpression from code behind. I tried this in my code, and I'm not seeing a behaviour difference. 

It's for Telerik 2010Q2 release, without the service pack. 
VS2010, .NET 4.

The relevant grid markup follows. It pulls from a LinqDataSource, declared by DataSourceID.

<MasterTableView DataKeyNames="ReportID">
       <SortExpressions>
           <telerik:GridSortExpression FieldName="ReportSeverityLevelID" SortOrder="Descending" />
           <telerik:GridSortExpression FieldName="ReportSeverityColumn" SortOrder="Descending" />
       </SortExpressions>
           <Columns>
               <telerik:GridTemplateColumn HeaderText="Severity" HeaderStyle-Width="45px" SortExpression="ReportSeverityLevelID" UniqueName="ReportSeverityColumn" AllowFiltering="false">
                   <ItemTemplate>
                           <asp:Image ID="Image3" runat="server" ImageUrl='<%# string.Format("~/Images/{0}", ReportSeverityLevel.GetReportSeverityLevel(r=>r.ReportSeverityLevelID==(int)Eval("ReportSeverityLevelID"),r=>r.IconImageFile)) %>' ImageAlign="AbsMiddle"
                           AlternateText='<%# ReportSeverityLevel.GetReportSeverityLevel(r=>r.ReportSeverityLevelID==(int)Eval("ReportSeverityLevelID"),r=>r.Name) %>' ToolTip='<%# ReportSeverityLevel.GetReportSeverityLevel(r=>r.ReportSeverityLevelID==(int)Eval("ReportSeverityLevelID"),r=>r.Name) %>' />
                   </ItemTemplate>
               </telerik:GridTemplateColumn>

Not, I tried the sort expression by column name just in case, as the documentation suggests it should be by the DataSource field, which the first SortExpression for FieldName ReportSeverityLevelID is. 

Thanks for any ideas. 

4 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 22 Sep 2010, 02:50 PM

Hello Sergey,

From the demo, you need to handle the RadGrid's SortCommand event and set AllowCustomSorting="true". Like so:

protected void RadGrid1_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
        {
            //Apply custom sorting
              
                GridSortExpression sortExpr = new GridSortExpression();
                switch (e.OldSortOrder)
                {
                    case GridSortOrder.None:
                        sortExpr.FieldName = e.SortExpression;
                        sortExpr.SortOrder = GridSortOrder.Descending;
  
                        e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                        break;
                    case GridSortOrder.Ascending:
                        sortExpr.FieldName = e.SortExpression;
                        sortExpr.SortOrder = RadGrid1.MasterTableView.AllowNaturalSort ? GridSortOrder.None : GridSortOrder.Descending;
                        e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                        break;
                    case GridSortOrder.Descending:
                        sortExpr.FieldName = e.SortExpression;
                        sortExpr.SortOrder = GridSortOrder.Ascending;
  
                        e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                        break;
                }
  
                e.Canceled = true;
                RadGrid1.Rebind();
              
  
              
                if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))
                {
                    GridSortExpression sortExpr = new GridSortExpression();
                    sortExpr.FieldName = e.SortExpression;
                    sortExpr.SortOrder = GridSortOrder.Ascending;
  
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
                }
              
        }

Your MasterTableView would look like this:

<MasterTableView DataKeyNames="ReportID" AllowCustomSorting="true">
         
           <Columns>
               <telerik:GridTemplateColumn HeaderText="Severity" HeaderStyle-Width="45px" SortExpression="ReportSeverityLevelID" UniqueName="ReportSeverityColumn" AllowFiltering="false">
                   <ItemTemplate>
                           <asp:Image ID="Image3" runat="server" ImageUrl='<%# string.Format("~/Images/{0}", ReportSeverityLevel.GetReportSeverityLevel(r=>r.ReportSeverityLevelID==(int)Eval("ReportSeverityLevelID"),r=>r.IconImageFile)) %>' ImageAlign="AbsMiddle"
                           AlternateText='<%# ReportSeverityLevel.GetReportSeverityLevel(r=>r.ReportSeverityLevelID==(int)Eval("ReportSeverityLevelID"),r=>r.Name) %>' ToolTip='<%# ReportSeverityLevel.GetReportSeverityLevel(r=>r.ReportSeverityLevelID==(int)Eval("ReportSeverityLevelID"),r=>r.Name) %>' />
                   </ItemTemplate>
               </telerik:GridTemplateColumn>

I hope that helps.
0
Sergey
Top achievements
Rank 2
answered on 22 Sep 2010, 03:13 PM
@Cori, thanks for the reply, I understand this appears to be possibly from code behind as linked in the demo, but what I'm asking, is what does this part refer to then: 

Declarative sort expressions

You can set default sort expression for the MasterTableView or a detail GridTableView directly in the ASPX declaration of the grid. Simply addGridSortExpression objects to the SortExpressions property collection of the table view.


Since in the code behind you're merely adding SortExpressions to the collection anyway, it would seem to make sense that you can just declare them in markup as the above suggests too? 
0
Cori
Top achievements
Rank 2
answered on 22 Sep 2010, 07:09 PM
Hello Sergey,

You need the code to perform what you want to achieve. The code-behind is what is telling the grid to set Descending as the first sort order when the user clicks on a column that is not sorted.

Although the code is adding SortExpressions, it cannot be done declaratively because the SortExpressions in the MasterTableView is only telling the RadGrid how to sort the columns on initial page load. So pretty much all it does is sort the columns you set in the SortExpressions collection and show the sort arrow on the respective column's header.

I hope that explains why it cannot be done declaratively.
0
Paniz
Top achievements
Rank 1
answered on 26 Nov 2013, 01:12 PM
Hello Cori,
Thank you so much, it works :)

User From Iran
Tags
Grid
Asked by
Sergey
Top achievements
Rank 2
Answers by
Cori
Top achievements
Rank 2
Sergey
Top achievements
Rank 2
Paniz
Top achievements
Rank 1
Share this question
or