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

[Solved] How to turn of automatic sorting?

4 Answers 192 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mink
Top achievements
Rank 1
Mink asked on 26 Aug 2009, 07:45 AM
Hi,

I'm using a RadGrid with advanced data binding, sorting and custom paging.
For the custom paging I dynamically build my query to the database (with Linq) so that only the results for the current page are retrieved.
But in order for the sorting to be correct when using paging the sorting has to be incorporated in the query as well (and before the paging). I also do this dynamically because the columns of the grid are created dynamically. So I read the lambda function to be used as an argument for the sorting from a data source.
So far everything is fine. But! As the column in question contains strings the grid knows how to order the page alphabetically on this column. And it does!, on top of the sorting I incorporated in the query...

For example, let's say I have a grid with only one column, containing first names. And let's say the only first names in the database are "Alice", "Bob" and "Claire" and that the page size of the grid is 2. I define the lambda function for ordering first names such that they are ordered by length rather than by alphabet (something like x => x.Length).
Now if I would let the grid be ordered in ascending order on the first names column I would like to have the following result:
1st page: Bob, Alice. 2nd page: Claire.
 However, the result is:
1st page: Alice, Bob. 2nd page: Claire.
It may not be immediately clear from this small example, but the result is that per page the records on it are exactly the records I want, but the order within the page is wrong due to unwanted extra automatic sorting, in this case in alphabetic order.

To be clear, I only use the NeedDataSource event and when creating the query for the data source I take care of the sorting and paging. I do not use the SortCommand event. I have tried this but I still couldn't get rid of the unwanted automatic sorting (this could very well be my inexpertice) and I started to have problems with my own implemented sorting to be one step behind. So when ASCending was requested you would get NONE, when DESCending was requested you get ASCending and when NONE was requested you get DESCending. This hase something to do with me reading from MasterTable.SortExpressions what sort is requested and using this information to dynamically get the right lambda function.

It would be really great if I could leave everything as I have it and only turn of the automatic sorting, without having to use the SortCommand event, because we have set up the project such that almost all code for using the grid is in seperate classes (such as a child class of RadGrid) so that a page using the grid only has to have minimal code (such as telling wich columns should be displayed, making the base query for the data source and define the lambda functions for sorting).
If it is unavoidable to use the SortCommand event please be very specific how this event and the NeedDataSource have to work together to avoid the sorting to be one step behind.
I rather have some extra code in the classes that take care of most things around the grid than any extra code in the pages using the grid because then I only have to write this code once instead of each time I use the grid on a page (which is going to happen a lot!).

Regards,

Mink

4 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 28 Aug 2009, 01:02 PM
Hello Mink,

In order to achieve your goal, I suggest that you try setting the AllowCustomSorting property in the grid MasterTableView settings to true. Then handle the sorting as described in this help article.

Regards,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Mink
Top achievements
Rank 1
answered on 31 Aug 2009, 03:14 PM
Alright, I'll give the SortCommand event another try.
As I said, I have tried this before (used the same example as you posted).
Although I'm sure I should be able to get it working this way, you haven't explicitly commented on my desire to not use this event if possible. This really is the only way?
0
Veli
Telerik team
answered on 03 Sep 2009, 01:41 PM
Hi Mink,

If both paging and sorting is incorporated in your data extraction logic, I believe you can skip using custom sorting and only rebind your grid in the SortCommand event. Thus, your sorting is delegated to your custom item selection and in RadGrid's NeedDataSource event, you can get both the page index and the sort expression to extract the correct data.

Kind regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Mink
Top achievements
Rank 1
answered on 10 Sep 2009, 01:48 PM
Hi Veli,

Thanks.
I already got it to work using the SortCommand event.
It turned out that my problem lied with the SortExpressions collection not being up to date until after the event is handled.
I used this collection to store initial sorting and to retrieve the intended sorting when dynamically creating my data retrieval. Because in case of the SortCommand event this collection was not up to date my sorting did not match the intended sorting.
However, updating the SortExpressions collection using the event argument resulted in the values of the event argument to update along with the changed SortExpressions values and as the values in the event argument are apparently used to automatically update the SortExpressions values after the event is handled this resulted in the SortExpressions collection being ahead instead of being not up to date. :-S
I tackled this problem by updating the SortExpressions according to the event argument values, dynamically creating my data retrieval and then restoring the SortExpressions to it original state so that after the even is handled the SortExpressions will be updated correctly. :-)
Maybe there is a better approach to this problem (maybe using something else than the SortExpressions collection as a source of information regarding intended sorting when dynamically creating the data retrieval?), but maybe this might actually be of help to someone else having these problems.

Mink
Tags
Grid
Asked by
Mink
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Mink
Top achievements
Rank 1
Veli
Telerik team
Share this question
or