What I am doing is handling my ICollectionView's SortDescriptions.CollectionChanged event and issuing a query on the server and then update the ICollectionView's source, which will cause an update on the grid. This works great since I can use the same logic to do server queries and in-memory queries if I want to bind my grid to a small list of entities and keep sorting, paging, filtering, etc.
That said, I have never noticed, not until now, that when I am sorting the grid also sorts my collection before my service returns the sorted data. How can I prevent this? I tried to handle the Sorting event and cancel it but then the SortDescriptions on my ICollectionView never get changed.
Thanks,
MF.
20 Answers, 1 is accepted
You could cancel the Sorting event and then manually add SortDescription to your ICollectionView. The event arguments of the Sorting event should provide you with all of the data that is needed to set up a new SortDescription.
Kind regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
That definitely sounds like a solution. Should I add the sort descriptions to the grid's ItemsSource (my ICollectionView) or to the grid's SortDescriptors collection? I suppose that adding it to the grid's SortDescriptors would synchronize my ICollectionView's SortDescriptors as well.
Does canceling the sorting event prevent the grid columns from displaying the sort directions above the header?
| static void grid_Sorting(object sender, GridViewSortingEventArgs sortingArgs) |
| { |
| var grid = sender as RadGridView; |
| sortingArgs.Cancel = true; |
| grid.SortDescriptors.SuspendNotifications(); |
| if (sortingArgs.IsMultipleColumnSorting) |
| { |
| SortDescriptor toRemove = grid.SortDescriptors.FirstOrDefault(s => s.Member == sortingArgs.SortPropertyName); |
| if (toRemove != null) |
| { |
| grid.SortDescriptors.Remove(toRemove); |
| } |
| } |
| else if (grid.SortDescriptors.Count > 0) |
| { |
| grid.SortDescriptors.Clear(); |
| } |
| SortDescriptor toAdd = GetSortDescriptor(sortingArgs); |
| if (toAdd != null) |
| { |
| grid.SortDescriptors.Add(toAdd); |
| } |
| grid.SortDescriptors.ResumeNotifications(); |
| } |
I used reflector to see how you handle the Sorting event and the SortDescriptors collection and modified it a little bit.
Works pretty well.
Thanks.
Adding to the grid's SortDescriptors collection will trigger the sorting mechanism of RadGridView - double sorting will still occur. I am not sure if you really need to use ICollectionView as ItemsSource. You can handle the Sorting event of RadGridView, cancel the event if you need to get data from the server, call the server, and update the ItemsSource property of RadGridView - that way you will not need to use ICollectionView as a proxy.
What do you think?
Kind regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Besides, my mechanism is 100% generic, I can use it with any kind of control that respects the ICollectionView, INotifyCollectionChanged, IPagedCollectionView and other interfaces as well.
I'm only using the RadGridView to display the entities in my collection, synchronize the selected item and update my ICollectionView's SortDescriptions so I can execute another query. The same goes for the DataPager.
Anyway, If I don't add the SortDescriptors to RadGridView's SortDescriptors collection but instead add it directly to my ICollectionView's SortDescriptions collection will RadGridView's column display the sort arrows above the headers??
Thanks
I see the column headers displaying the correct sort direction arrows but the grid keeps sorting for me. Wouldn't it be better if the grid had a property like SortMode with values like Default | Custom and in custom mode you would keep the same behavior, but just not perform the sorting...
Well, when RadGridView is bound to ICollectionView the grid will listen for changes in SortDescriptions and update its own SortDescriptors when a change occurs. This of course will cause the grid to perform sorting when SortDescriptions is changed. Unfortunately this behavior cannot be disabled.
I hope that the performance is good enough even with double sorting.
We had a discussion about your scenario and we have decided to introduce some changes that will allow you to have a full control over the sorting behavior of RadGridView. It is still unknow when we will be able to introduce those changes but we will try to do it as soon as possible.
I have updated your Telerik points. Thank you for your feedback.
Regards,Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
That's wonderful news. Thank you very much. If you need someone to test it just let me know.
Best regards,
Manuel FelĂcio.
We are also experiencing this problem when we are using our own custom implementation of ICollectionView (Using SortDescriptors).
The grid does a second sort on the data and because of that our custom sorting based on our requirements from customer can not be developed. We need to be able to disabled the second sort asap!
Has this been implemented in lastest release? If so, how do we disable the second sort?
If not, what timeframe do you have for implementing this?
Best Regards
Mattias
Why you use SortDescriptors for the grid if you have your own sorting?
Best wishes,Vlad
the Telerik team
We are binding RadGridView to collections that implements ICollectionView through RadPager and our ViewModels. This allows us to use MVVM pattern and use bindings to sort, page and group our data. (we are not using any code-behinds at all, no events either for that matter). This keeps the seperation of concerns in order and our GUI is highly pluggable :)
When something happens in the Grid, for example the user group or sort we fetch data using Silverlight WCF Services that are generated (T4) by use-case with a DTO model etc. (no we are not using WCF RIA services). The information is then transfered to backend where we manage the actual sorting, grouping and paging with LINQ-to-NHibernate to prevent sending to much data over the wire! This also gives us the ability to use our server-side business rules or customer specific requirements (sorting in this case), and keeping them pluggable using IoC containers.
That's the short version of our architecture building on DDD, SOA and MVVM among other patterns.
Just trust me when I say we need to disable the internal sorting of the grid!!
Regards
Mattias
In this case maybe it will be better to check my blog post which is very similar to your case.
Sincerely yours,Vlad
the Telerik team
Hi!
No, your blog does not show a solution to disable the internal sorting or RadGridView! Which is the only thing I asked about!?
Earlier in this thread Milan from Telerik team wrote this,
"Well, when RadGridView is bound to ICollectionView the grid will listen for changes in SortDescriptions and update its own SortDescriptors when a change occurs. This of course will cause the grid to perform sorting when SortDescriptions is changed. Unfortunately this behavior cannot be disabled.
I hope that the performance is good enough even with double sorting.
We had a discussion about your scenario and we have decided to introduce some changes that will allow you to have a full control over the sorting behavior of RadGridView. It is still unknow when we will be able to introduce those changes but we will try to do it as soon as possible."
That was 1,5 month ago! And the only thing I'm asking for if is this (the bold text) has been implemented, or when you expect it to be implemented?
Regards
Mattias
Have you checked CreateView() method in MyQueryableCollectionView from the project in my blog post? The issue with ICollectionView is not fixed in our current version and we will do our best to provide fix as soon as possible.
Greetings,Vlad
the Telerik team
Best regards,
MF.
Thank you for answering my question :)
Regards
Mattias
I want to use internal sorting of the gridview, but I need to also control it from the view model.
For example, when I first retrieve the grid items, I want to specify a default sorting and I want the grid view to highlite the column and the sort direction. Depending on the type of search, the default sort coulumn could be different. I also want to let the user sort by clicking on the column header.
Let's say I have 2 buttons, one "search today items", another one "search all items".
For "search today items" I want the default sort column to be "LastName", but for "search all items" I want the default sort column to be "Date".
I tryied to implement this using the PagedCollectionView class, but for some reason, when I replace the ItemsSource, the gris still keeps the old SortDescriptors and apply the new one on top of it even though I explicitly clear the SortDescriptors.
Any help would be appreciated.
Regards,
Jesus
You can add desired button on the column header using Header property and on button click add desired SortDescriptor.
All the best,Vlad
the Telerik team
Have you implemented such mechanisms that allow us to control the sorting behavior of RadGridView? It's very annoying to see the grid sorting twice..
Best regards,
Manuel FelĂcio.
I am sorry for my late reply. We have modified the behavior of RadGridView so that it won't perform sorting internally as long as its ICollectionView source supports sorting (i.e. its CanSort property is True). You will be able to find it in this week's Internal Build.
Kind regards,
Yavor Georgiev
the Telerik team
