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

Switching ColumnSortDescriptor dynamically

3 Answers 45 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jerry
Top achievements
Rank 1
Jerry asked on 02 Dec 2015, 09:46 PM

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);

 

 

3 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 03 Dec 2015, 01:27 PM
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
0
Jerry
Top achievements
Rank 1
answered on 03 Dec 2015, 01:48 PM

Yes, that works perfect!

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

 

0
Stefan Nenchev
Telerik team
answered on 03 Dec 2015, 03:37 PM
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
Tags
GridView
Asked by
Jerry
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Jerry
Top achievements
Rank 1
Share this question
or