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

[Solved] Custom Sorting sorts twice

20 Answers 308 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Manuel Felício
Top achievements
Rank 1
Manuel Felício asked on 04 Jun 2010, 04:35 PM
Hi,

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

Sort by
0
Milan
Telerik team
answered on 08 Jun 2010, 06:17 AM
Hello Manuel FelĂ­cio,

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.
0
Manuel Felício
Top achievements
Rank 1
answered on 08 Jun 2010, 10:03 AM
Hi,

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?
0
Manuel Felício
Top achievements
Rank 1
answered on 08 Jun 2010, 02:21 PM
Ok, I got it to work like this:

        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.
0
Milan
Telerik team
answered on 09 Jun 2010, 08:24 AM
Hello Manuel FelĂ­cio,

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.
0
Manuel Felício
Top achievements
Rank 1
answered on 09 Jun 2010, 02:14 PM
Are you sure? It's so fast I didn't even notice that.. well..I find re-setting the ItemsSource on the RadGridView a bit of a hack.. the only thing that needs to happen are INotifyCollectionChanged events that will be raised after I sort my source collection.

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
0
Manuel Felício
Top achievements
Rank 1
answered on 09 Jun 2010, 02:41 PM
I changed my code to add the SortDescriptions directly in my collectionview and the RadGridView's SortDescriptors gets updated as well, I guess INotifyCollectionChanged events are flowing in both directions.

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...
0
Milan
Telerik team
answered on 14 Jun 2010, 04:33 PM
Hello Manuel FelĂ­cio,

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.
0
Manuel Felício
Top achievements
Rank 1
answered on 17 Jun 2010, 06:31 PM
Hello Milan,

That's wonderful news. Thank you very much. If you need someone to test it just let me know.

Best regards,

Manuel FelĂ­cio.
0
Centiro Developer
Top achievements
Rank 1
answered on 05 Aug 2010, 12:20 PM
Hi!

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
0
Vlad
Telerik team
answered on 05 Aug 2010, 12:58 PM
Hello Mattias,

 Why you use SortDescriptors for the grid if you have your own sorting?

Best wishes,
Vlad
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
0
Centiro Developer
Top achievements
Rank 1
answered on 05 Aug 2010, 01:31 PM
Hi!

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
0
Vlad
Telerik team
answered on 05 Aug 2010, 02:44 PM
Hello 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
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
0
Centiro Developer
Top achievements
Rank 1
answered on 05 Aug 2010, 03:01 PM

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
0
Vlad
Telerik team
answered on 05 Aug 2010, 03:10 PM
Hi,

 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
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
0
Manuel Felício
Top achievements
Rank 1
answered on 05 Aug 2010, 03:16 PM
Hi, I'm looking forward to it as well.

Best regards,

MF.
0
Centiro Developer
Top achievements
Rank 1
answered on 05 Aug 2010, 03:26 PM

Thank you for answering my question :)

Regards
Mattias
0
Alexander Kotchkin
Top achievements
Rank 1
answered on 17 Aug 2010, 04:08 PM
I am having a similar problem.
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
0
Vlad
Telerik team
answered on 20 Aug 2010, 12:16 PM
Hello,

 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
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
0
Manuel Felício
Top achievements
Rank 1
answered on 16 Nov 2010, 12:26 PM
Hello again,

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.
0
Yavor Georgiev
Telerik team
answered on 23 Nov 2010, 02:32 PM
Hello 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
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
GridView
Asked by
Manuel Felício
Top achievements
Rank 1
Answers by
Milan
Telerik team
Manuel Felício
Top achievements
Rank 1
Centiro Developer
Top achievements
Rank 1
Vlad
Telerik team
Alexander Kotchkin
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Share this question
or