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

[Solved] Problems with custom sorting

2 Answers 140 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.
Dario
Top achievements
Rank 1
Dario asked on 17 Aug 2009, 02:43 PM
I ran into some problems while trying to implement a custom sorting option on my custom column class. I've extended the GridViewDataColumn class and in order to make my column sortable I have overridden "CanSort()" method to always return true.
The point is, I only want to use the UI functionality of the grid, but then I want  to intercept the "Sorting" event, do my custom sorting server side, and cancel the grid's "sorting" implementation.
This is all well until  I get get the exception saying my data must implement IComparable, and that is a problem for me, because I use some dynamic data as a grid's source and cannot implement IComparable. Also, because I only want to catch sorting event, and not use the logic behind it, I hope I can somehow overcome this problem. Can I?

2 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 20 Aug 2009, 10:12 AM
Hello Dario ,

Since you cannot implement IComparable, you will need to cancel the sorting like this:

using System.Windows.Controls; 
using Telerik.Windows.Controls; 
 
namespace TicketID_235633_CustomSortingProblems 
    public partial class MainPage : UserControl 
    { 
        public MainPage() 
        { 
            this.InitializeComponent(); 
 
            var cc = new CustomColumn(); 
            this.playersGrid.Columns.Add(cc); 
            this.playersGrid.Sorting += OnSorting; 
            this.playersGrid.ItemsSource = Club.GetPlayers(); 
        } 
 
        private static SortingState GetNextSortingState(SortingState currentState) 
        { 
            switch (currentState) 
            { 
                case SortingState.None: 
                    return SortingState.Ascending; 
                case SortingState.Ascending: 
                    return SortingState.Descending; 
                case SortingState.Descending: 
                    return SortingState.None; 
                default
                    return SortingState.None; 
            } 
        } 
 
        private static void OnSorting(object sender, GridViewSortingEventArgs e) 
        { 
            if (e.Column.GetType() == typeof (CustomColumn)) 
            { 
                e.Cancel = true
                e.SortingState = GetNextSortingState(e.SortingState); 
            } 
        } 
    } 


Best wishes,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Dario
Top achievements
Rank 1
answered on 20 Aug 2009, 11:42 AM
Tnx Ross, it works now :)
Tags
GridView
Asked by
Dario
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Dario
Top achievements
Rank 1
Share this question
or