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

Default Sort Order Anytime Field is Sorted

12 Answers 171 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Levi
Top achievements
Rank 1
Levi asked on 28 Jan 2009, 01:44 AM
Is there a way to have the grid default to Descending when a sort is used. I don't want the grid to come up sorted by default. I simply want it to use Descending first as the default sort direction. I was surprised that there wasn't a property in the sort settings to do this. Am i missing something?

12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Jan 2009, 05:45 AM
Hello Levi,

You can try out the following code to set the the default sort order to descending, on sorting a column:
cs:
protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e) 
    { 
        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); 
        } 
    } 

You can also refer to the following online demo for more information on the Sorting Feature:
Basic sorting

Thanks
Princy.
0
Levi
Top achievements
Rank 1
answered on 28 Jan 2009, 06:06 AM
I tried that code but it didn't work. The sorting just breaks.
0
Levi
Top achievements
Rank 1
answered on 20 Mar 2009, 03:17 PM
Any word from Telerik on this? This seems like a pretty common operation.
0
Sebastian
Telerik team
answered on 20 Mar 2009, 03:23 PM
Hi Levi,

For this purpose you can use either declarative or programmatic sort expressions as explained in this help article:

http://www.telerik.com/help/aspnet-ajax/grdsortingexpressions.html

This is demonstrated in several online demos of the grid and I am linking a couple of them for your further reference:

http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/declarativerelations/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/nestedviewtemplate/defaultcs.aspx (the most inner table)

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Levi
Top achievements
Rank 1
answered on 20 Mar 2009, 03:27 PM
I don't want a default sort. I want it to automatically sort descending (rather than ascending) the first time a sort is performed on a field. I'm surprised that there is not a property to do this.
0
Sebastian
Telerik team
answered on 20 Mar 2009, 03:33 PM
Hello Levi,

Thank you for the clarification. This can be done with the code provided by Princy or following the steps from this help topic. Declarative settings is not present yet, however I will forward your suggestion to our development team to be considered for future versions. Thank you for your feedback.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Levi
Top achievements
Rank 1
answered on 20 Mar 2009, 03:36 PM
As i said in my follow up post to Princy, the code provided did not work. It only breaks the sorting.Perhaps there are some additional properties I need to set? Any reason why the code should not work?

Levi
0
Levi
Top achievements
Rank 1
answered on 20 Mar 2009, 03:53 PM
Essential i need to know if the previous sort was "NO SORT", then set it to DESC. But just setting it to descending every time the event gets raised will not work for me.
0
Sebastian
Telerik team
answered on 20 Mar 2009, 03:58 PM
Hi Levi,

Have you reviewed the implementation from the help topic linked in my previous reply? I tested it locally and it works as expected. Feel free to modify/extend the logic to meet your custom requirements to control the grid sorting modes.

Kind regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Levi
Top achievements
Rank 1
answered on 20 Mar 2009, 04:59 PM
I got the following code to work (which is similar to the link you sent), although it doesn't work with custom sorting enabled. As a matter of fact it doesn't like me messing with the sorting expressions at all when I enable the custom sorting.

            GridTableView tableView = e.Item.OwnerTableView;

            e.Canceled = true;
            GridSortExpression expression = new GridSortExpression();
            expression.FieldName = e.SortExpression;

            if (tableView.SortExpressions.Count == 0 || tableView.SortExpressions[0].SortOrder == GridSortOrder.None)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
            {
                expression.SortOrder = GridSortOrder.Ascending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
            {
                expression.SortOrder = GridSortOrder.None;
            }
            tableView.SortExpressions.AddSortExpression(expression);
            tableView.Rebind();

>>>>

This is great code for anyone who doesn't need the custom sorting. I only briefly tested but seems to work fine.

>>>>

Normally i just do the following in places i have custom sorting enabled, which works great but i have no control over the sorting directions:

  protected void RadGridSummary_SortCommand(object source, GridSortCommandEventArgs e)
        {
            // store new sort expression in hidden field
            SortExpression.Value = string.Format("{0} {1}", e.SortExpression, string.IsNullOrEmpty(e.SortExpression) ? "" : e.NewSortOrder == GridSortOrder.Ascending ? "asc" : "desc");
            LoadData();
        }

        void LoadData()
        {
            try
            {
                int startRowIndex = RadGridSummary.MasterTableView.CurrentPageIndex * RadGridSummary.MasterTableView.PageSize;
                int maximumRows = RadGridSummary.MasterTableView.PageSize;
                ReferrerBySiteData data = Referrer.GetBySite(SiteId, DateFilter, SortExpression.Value, startRowIndex, maximumRows);
                RadGridSummary.VirtualItemCount = data.ReferrerDataList.Count == 0 ? 0 : data.ReferrerDataList[0].TotalRows.Value;
                RadGridSummary.DataSource = data.ReferrerDataList;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

>>>

Is there anyway for me to control the sorting direction and use custom sorting? I use the custom sorting for database performance (i.e.: grab 10 records)
...

Another problem... I already store the sort in a hidden field because it doesn't like me messing with the sort expression with the custom sorting enabled.

Thanks!


0
Levi
Top achievements
Rank 1
answered on 20 Mar 2009, 06:17 PM
Sorry to spam this thread, but fixed a bug with switching fields and it continuing the sort form previous field. Also no longer using a form variable to store the sort as this is no longer needed due to the rebind (which was missing from my previous experiments):

Working solution for default sort direction with custom sort enabled is below. This code does not use "no sort", but this can be changed in the case statement.

 protected void RadGridSummary_SortCommand(object source, GridSortCommandEventArgs e)
        {
            GridTableView tableView = e.Item.OwnerTableView;
            bool bNewField = (e.SortExpression != tableView.SortExpressions[0].FieldName);
            
            e.Canceled = true;
            
            GridSortExpression expression = new GridSortExpression();
            expression.FieldName = e.SortExpression;

            if (tableView.SortExpressions.Count == 0 || tableView.SortExpressions[0].SortOrder == GridSortOrder.None || bNewField)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
            {
                expression.SortOrder = GridSortOrder.Ascending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }
            tableView.SortExpressions.AddSortExpression(expression);
            tableView.Rebind();            
        }
0
Sebastian
Telerik team
answered on 24 Mar 2009, 09:28 AM
Hello Levi,

I am glad that you discovered a solution which suits your needs when using custom sorting - thank you for sharing it in this public forum thread. Thus you can help other people who are searching similar implementation. I updated your Telerik points for the involvement.

Greetings,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Levi
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Levi
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or