Hi,
I'm using RadDataPager with RadGridView. I need to access sorted source in RadDataPager after click on column header of my RadGridView.
Below is my code:
<telerik:RadGridView ItemsSource="{Binding PagedSource, ElementName=pager}" CanUserSortColumns="True" AutoGenerateColumns="True" />
<telerik:RadDataPager x:Name="pager" PageSize="25" Source="{Binding RecordsItemsSource}"
IsTotalItemCountFixed="False"
DisplayMode="All"
NumericButtonCount="10" />
Where RecordsItemsSource is ObservableCollection in my ViewModel.
Thanks in advance,
Kris
5 Answers, 1 is accepted
RadDataPager itself does not hold a sorted collection. You can access the same from RadGridView.Items. You can also find the current sort descriptors applied through
RadGridView.SortDescriptors collection.
Regards,
Dimitrina
Telerik
Thanks Dimitrina for your reply.
In RadGridView.Items I can find items only in actual page, not all items.
I tried to use RadGridView.SortDescriptors and CollectionViewSource to sort base collection. Unfortunately sorting by RadGridView gives me diffrent collection for this same values. For example:
Name: John, Age: 17
Name: Piter, Age: 16
Name: Rita, Age: 15
Name: Jenny, Age: 16
When I sort on "Age"
RadGridView return: Rita, Jenny, Piter, John
CollectionViewSource with RadGridView.SortDescriptors return: Rita, Piter, Jenny, John
If "RadDataPager itself does not hold a sorted collection", how RadDataPager knew which items gives to RadGridView on actual page? RadDataPager sort base collection on every time when I change page?
Thanks in advance,
Kris
I will try to explain the way RadDataPager works in some further details. Every time you navigate to a page, it internally builds a LINQ query appending Skip and Take clauses and then this query is to be executed on the server. When binding the pager to a LINQ-enabled data source (i.e. IQueryable), the paging will happen on the server out-of-the-box. In case the pager is bound to an IQueryable, it will automatically append Skip(n) and Take(m) statements to this query.
Similarly, sorting in RadGridView is a data operation and we internally generate and execute a LINQ query appending a OrderBy clause to the source collection.
var result = GridViewSourceCollection.OrderBy(x => x.Description);
Then, when using sorting and paging, there is a proper LINQ query built, the method 'OrderBy' must be called before the method 'Skip'.
Regards,
Dimitrina
Telerik
I have a similar situation for this issue and I am able to get the Sort Direction (Ascending or Descending) of the Column but not able to get the Column Name that is being Sorted (Customer Name or Account Number). How are you able to get this information? I can see the Column Name while in Debug Mode but not able to retrieve it.
Thanks!
You can access it like so:
ColumnSortDescriptor sorting =
this
.clubsGrid.SortDescriptors.FirstOrDefault()
as
ColumnSortDescriptor;
if
(sorting !=
null
)
{
var column = sorting.Column
as
GridViewDataColumn;
string
name = column.Header.ToString();
}
I hope this helps.
Regards,
Dimitrina
Telerik