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

Initial sorting

8 Answers 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 19 Oct 2011, 08:28 AM
Hello,
I want to make an initial sorting when grid is loaded first but, when I press column header to sort, I want to dismiss initial sorting expressions and follow the grid default sorting procedures. Is that possible? I already know how to sort programmatically but I cannot correctly remove sorting expressions when I don't need them.
Thank you.

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Oct 2011, 08:47 AM
Hello Dmitry,

You can clear the initial sorting expression in PreRender event as shown below.
C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
  GridSortExpression expression = new GridSortExpression();
  expression.FieldName = "ColumnUniqueName";
  expression.SortOrder = GridSortOrder.None;
  RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression);
}

Thanks,
Shinu.
0
Dmitry
Top achievements
Rank 1
answered on 19 Oct 2011, 09:36 AM
Hello Shinu,
Thank you for reply but I'm not sure how sort expression can be cleared by adding new expression, and moreover, when all expressions have already been applied?
0
Dmitry
Top achievements
Rank 1
answered on 20 Oct 2011, 02:16 PM
Hello,
Any workaround please?
0
Dmitry
Top achievements
Rank 1
answered on 27 Oct 2011, 12:29 PM
Hello,
Can anybody please help me with this issue?
0
Dmitry
Top achievements
Rank 1
answered on 31 Oct 2011, 12:55 PM
Is anyone responding on this forum?
0
Sebastian
Telerik team
answered on 02 Nov 2011, 03:32 PM
Hello Dmitry,

What Shinu proposed it to set GridSortOrder.None for the initially sorted grid column (you need to replace  'ColumnUniqueName' with the unique name of the actual column). Thus when you modify the SortExpressions collection of the grid, the initially applied sort expression will be cleared. Probably the more appropriate place would be to do that intercepting the OnDataBound event of the grid instead of its PreRender event.

Another approach would be to clear the SortExpressions collection each time a new sort expression is applied, to reflect solely the last sort order chosen by the user. For more information please refer to this help article.

Regards,
Sebastian
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Dmitry
Top achievements
Rank 1
answered on 08 Nov 2011, 02:11 PM
Thank you for reply.
I tried to do what Shinu have proposed but this works as follows:
1. Grid loads with initial sorting (code below)
2. I click any column header to sort it. Sort will not change and predefined sorting will still apply
3. I click the same column header again and THIS time it will sort as it should.
Below is code I use. It's all in Grid_DataBound event handler.
What am I doing wrong?

UPD. I made a mistake, sorry. When I click column header 1st time, all sorting will be cleared but new sorting order will not apply.
UPD2. Sorry again. Both issues are right in my case. I cannot say, why sometimes it will clear initial sorting, and why sometimes it will not.

 if (!Page.IsPostBack)
            {
                foreach (var sortExpression in _columnBuilder.SortExpressions)
                {
                    MasterTableView.SortExpressions.AddSortExpression(sortExpression);
                }
            }
            else
            {
                GridSortExpression expression = new GridSortExpression
                                                    {
                                                        FieldName = "id", 
                                                        SortOrder = GridSortOrder.None
                                                    };
                MasterTableView.SortExpressions.AddSortExpression(expression);
            }
0
Dmitry
Top achievements
Rank 1
answered on 08 Nov 2011, 03:52 PM
Solution was simple.
In ItemCommand handler:

switch (e.CommandName)
            {
                case SortCommandName:
                    MasterTableView.SortExpressions.Clear();
                    break;
            }
Thank you.
Tags
Grid
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Dmitry
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or