Switching ColumnSortDescriptor dynamically

4 posts, 0 answers
  1. Jerry
    Jerry avatar
    7 posts
    Member since:
    Mar 2013

    Posted 02 Dec 2015 Link to this post

    I have a Silverlight app with a telerik:RadGridView.

    Depending on the query the users selects I want to sort a certain column. Only 1 column at a time.

    In my code behind, I do the below. This works the first time, but when the other option is select and sort 2 is set, both columns are set to be sorted.

    How do I only sort on one column at a time?

     

         ColumnSortDescriptor csd = new ColumnSortDescriptor();

         if (Mycheckbox.IsChecked == true)

    {

    csd.Column = MyGrid.Columns["Column1"];

    csd.SortDirection = System.ComponentModel.ListSortDirection.Descending;

    }

    else

    {

    csd.Column = MyGrid.Columns["Column2"];
    csd.SortDirection = System.ComponentModel.ListSortDirection.Descending;

    }

    MyGrid.SortDescriptors.Add(csd);

     

     

  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    277 posts

    Posted 03 Dec 2015 Link to this post

    Hi Jerry,

    When you initially create one of the SortDescriptors and you add it to the SortDescriptors collection of the RadGridView it stays in the collection unless you clear it. So in your case when you sort column1 and you then decide to sort column2, the SortDescriptors collection already contains the descriptor for the first column. In order to avoid such behavior you need to clear the SortDescriptors collection as in the code snippet below:

    ColumnSortDescriptor csd = new ColumnSortDescriptor();
    if (Mycheckbox.IsChecked == true)
    {
     MyGrid.SortDescriptors.Clear();
     csd.Column = MyGrid.Columns["Column1"];
     csd.SortDirection = System.ComponentModel.ListSortDirection.Descending;
     MyGrid.SortDescriptors.Add(csd);
    }
    else
    {
     MyGrid.SortDescriptors.Clear();
     csd.Column = MyGrid.Columns["Column2"];
     csd.SortDirection = System.ComponentModel.ListSortDirection.Descending;
     MyGrid.SortDescriptors.Add(csd);
    }

    Please, test this approach and update me if it works well for you.

    Regards,
    Stefan Nenchev
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Jerry
    Jerry avatar
    7 posts
    Member since:
    Mar 2013

    Posted 03 Dec 2015 in reply to Stefan Nenchev Link to this post

    Yes, that works perfect!

    Telerik may want to include that in the Sorting examples/documentation.

     

  5. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    277 posts

    Posted 03 Dec 2015 Link to this post

    Hello Jerry,

    I am glad to hear that the previously suggested implementation was useful at your end. Indeed, it is a good idea to include this approach in our documentation as it might be time-consuming for our customers to figure it out. We will consider adding it in the near future. I have added 500 points to your Telerik account as well.

    Regards,
    Stefan Nenchev
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top