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

[Solved] Customised sorting issues

1 Answer 151 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.
Przemyslaw
Top achievements
Rank 1
Przemyslaw asked on 10 Nov 2009, 01:59 PM
Hi,

Player_AllPlayers_GridView control inherits from the RadGridView and its ItemSource is set to an ObsarvableCollection maintained in a web service tier. In general everything works properly. I just cannot get control over my customised sorting. Since, I am using my own paging scheme, I would like to capture the Sorting event. Therefore, I am attaching a handler for it (see below). Then, in the handler I retrieve the items that I am interested in which are already sorted on the server. The RadGridView does display the new items correctly. Unfortunatelly, it does its OWN sorting beforhand. Setting e.Handled = true does not change anything, the "internal" sorting still happens first and my sorting afterwards.
The second question is about the DataLoaded event. It is executed after each row is loaded for the RadGridView. Is there an event that is executed (only once) after all rows have been loaded?

public partial class MainContent()
{
    Player_AllPlayers_GridView grid;
    [...]

    public  MainContent()
    {
        InitializeComponent();
  
        //I am feeding an ObservableCollection reference to the RadGridView
        grid = new Player_AllPlayers_GridView(MainPage.webservices.WebService.ProfileList);
        grid.DataLoaded += grid_DataLoaded;
        grid.Sorting += grid_Sorting;
    }
}

void grid_Sorting(object sender, Telerik.Windows.Controls.GridViewSortingEventArgs e)
{
    string sortDesc = e.SortPropertyName;
    if (e.NewSortingState == Telerik.Windows.Controls.SortingState.Descending)
        sortDesc += "_desc";
    // Here I am just repopulating the ObservableCollection that is assigned to this grid
    HeaderClickRetrieveSortedProfiles(sortDesc);
    e.Handled = true;
}

void grid_DataLoaded(object sender, EventArgs e)
{
        [...]
}

regards, Przemek

ps. I am sorry, this question was posted to WPF forum initially, but it is targeting Silverlight Q3

1 Answer, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 11 Nov 2009, 10:35 AM
Hello Przemyslaw,

You should Cancel the event and then manually set the column's SortingState (the little UI triangle that is displayed) since you will be doing sorting by hand.

The DataLoaded event will be fired each time you sort, because you are effectively modifying the data source and the grid reloads the data.

I have prepared a small sample project that demonstrates all of this. I am using our QueryableCollectionView to simulate your custom sorting logic. In other words, when we enter the sorting event I tell the grid to Cancel and I do the sorting by hand on the QueryableCollectionView. Then I simply update the column SortingState to the correct one.

Here is part of the code:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
 
namespace TicketID_257389_CustomSorting
{
    public partial class MainPage : UserControl
    {
        private QueryableCollectionView dataSource;
         
        public MainPage()
        {
            InitializeComponent();
 
            this.dataSource = new QueryableCollectionView(Club.GetPlayers());
 
            this.playersGrid.Sorting += this.OnPlayersGridSorting;
            this.playersGrid.DataLoaded += (s, e) => MessageBox.Show("DataLoaded");
 
            this.playersGrid.ItemsSource = this.dataSource;
        }
 
        void OnPlayersGridSorting(object sender, GridViewSortingEventArgs e)
        {
            SortDescriptor toRemove = null;
            foreach (SortDescriptor descriptor in this.dataSource.SortDescriptors)
            {
                if (descriptor.Member == e.SortPropertyName)
                {
                    toRemove = descriptor;
                    break;
                }
            }
 
            if (toRemove != null)
            {
                this.dataSource.SortDescriptors.Remove(toRemove);
            }
             
            // Simulate the repopulating the ObservableCollection that is assigned to this grid
            switch (e.NewSortingState)
            {
                case SortingState.Ascending:
                    this.dataSource.SortDescriptors.Add(new SortDescriptor
                                                            {
                                                                Member = e.SortPropertyName
                                                                , SortDirection = ListSortDirection.Ascending
                                                            });
                    break;
                case SortingState.Descending:
                    this.dataSource.SortDescriptors.Add(new SortDescriptor
                                                            {
                                                                Member = e.SortPropertyName,
                                                                SortDirection = ListSortDirection.Descending
                                                            });
                    break;
            }
             
            e.Cancel = true;
            e.Column.SortingState = e.NewSortingState;
        }
    }
}

Please find the whole project attached. Let me know if you have any other questions.

Regards,
Ross
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.
Tags
GridView
Asked by
Przemyslaw
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or