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

How to initialize sort Gridview on multiple columns

5 Answers 465 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.
Remco
Top achievements
Rank 1
Remco asked on 01 Aug 2011, 01:17 PM
Hi,

We allow our users to sort a Gridview by 3 columns max. When the user selects another Gridview we're saving the sort-columns and order of the active Gridview. When the user reselects a previous Gridview we want to restore the sort-columns. This works fine if the Gridview is only sorted by 1 column. But when the user sorted the previous Gridview by multiple columns, only the SortingState of the last sort-column is visible to the user.

FYI. The query, sorting and paging is done using a webservice which performs the requested query on the database. The result page data is returned and bound to the ItemsSource. The Gridview sorting is performed by requerying the database using an event handler on the Gridview.Sorting. 

Could you instruct us how we can make all SortingState visible to the user during the Gridview initialization, see source code below
?

Regards, Remco 

this.ListView.ItemsSource = queryResult;
 
for (int i = 0; i <= 2; i++)
{
   if (!string.IsNullOrEmpty(sortColumnName[i]))
   {
      Telerik.Windows.Data.SortDescriptor descriptor = new Telerik.Windows.Data.SortDescriptor();
      descriptor.Member = sortColumnName[i];
      descriptor.SortDirection = sortColumnOrder[i] == "ASC" ? System.ComponentModel.ListSortDirection.Ascending : System.ComponentModel.ListSortDirection.Descending;
      this.ListView.SortDescriptors.Add(descriptor);
      GridViewColumn columnSort = this.ListView.Columns[sortColumnName[i]];
      columnSort.SortingState = sortColumnOrder[i] == "ASC" ? SortingState.Ascending : SortingState.Descending;
   }
 
}


 

5 Answers, 1 is accepted

Sort by
0
Remco
Top achievements
Rank 1
answered on 02 Aug 2011, 10:43 PM
After rereading the online manual once again I decided not to use SortDescriptor, but the suggested alternative ColumnSortDescriptor to intialize and manage our Gridview's multiple column sort during runtime. This seems to work as I would expect, see adapted source code. Unfortunately the documentation is not very clear at this point.

this.ListView.ItemsSource = queryResult;
 
for (int i = 0; i <= 2; i++)
{
  if (!string.IsNullOrEmpty(sortColumnName[i]))
  {
    Telerik.Windows.Controls.GridView.ColumnSortDescriptor descriptor = new Telerik.Windows.Controls.GridView.ColumnSortDescriptor()
    {
      Column = this.ListView.Columns[sortColumnName[i]],
      SortDirection = sortColumnOrder[i] == "ASC" ? System.ComponentModel.ListSortDirection.Ascending : System.ComponentModel.ListSortDirection.Descending
    };
    this.ListView.SortDescriptors.Add(descriptor);
  }
 
}





0
Dimitrina
Telerik team
answered on 03 Aug 2011, 07:32 AM
Hi Remco,

 Indeed in order to sort multi columns programmatic, you will need to add multiple column sort descriptors to the collection, like you have found. You may check the programmatic sorting  for more info. 


Thank you for this feedback, we are going to update our Help so that the multiple-columns sorting article references to the the programmatic sorting.

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Remco
Top achievements
Rank 1
answered on 03 Aug 2011, 07:57 AM
Hello Didie,

The multiple-columns sorting article already has a reference to this page, bit the confusing part is the 'Programmatic Sorting' page. 

The first example uses the SortDescriptor, but with this approach the column sorting is not shown, you should use the later proposed ColumnSortDescriptor alternative in order make it all work. 

Regards, Remco
0
Remco
Top achievements
Rank 1
answered on 03 Aug 2011, 11:02 AM
Didie,

I just noticed a strange sorting behaviour, when handling the Gridviews Sorting event programmatically. Although we set the e.Cancel property to True, the Gridview is always sorted internally if you update the SortDescriptors collection.

We want to be able to sort the data programmatically by requerying the database and updating the Gridview.ItemsSource. The Column header Sort state is only cosmetic and show the user how the Gridview is sorted.  

How can we prevent the Gridview from being sorted internally, as the e.Cancel does not seem to work as described? 


0
Dimitrina
Telerik team
answered on 03 Aug 2011, 03:31 PM
Hello Remco,

I see now, thank you again for the clarification. 
We have checked how the SortDescriptors are applied programmatic, but unfortunately we were not able to reproduce a behavior like yours. If you want you may send us a small running sample(via a support ticket) showing the problem scenario. 

 Generally it is better to use the ColumnSortDescriptor as they specify the particular column to sort. When the user sort from the UI RadGridView as well adds ColumnSortDescriptor to its SortDescriptors collection.
 

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
Remco
Top achievements
Rank 1
Answers by
Remco
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or