Telerik blogs

RadDataPager

We are glad to announce that the Q1 2010 Release has added another weapon to RadGridView’s growing arsenal of features. This is the brand new RadDataPager control which provides the user interface for paging through a collection of data.

The good news is that RadDataPager can be used to page any collection. It does not depend on RadGridView in any way, so you will be free to use it with the rest of your ItemsControl’s if you chose to do so.

Before you read on, you might want to download the samples solution that I have attached. It contains a sample project for every scenario that I will discuss later on. Looking at the code while reading will make things much easier for you. There is something for everyone among the 10 Visual Studio projects that are included in the solution. So go and grab it.

I. Paging essentials

The single most important piece of software concerning paging in Silverlight is the System.ComponentModel.IPagedCollectionView interface. Those of you who are on the WPF front need not worry though. As you might already know, Telerik’s Silverlight and WPF controls is share the same code-base. Since WPF does not contain a similar interface, Telerik has provided its own Telerik.Windows.Data.IPagedCollectionView.

The IPagedCollectionView interface contains several important members which are used by RadGridView to perform the actual paging. Silverlight provides a default implementation of this interface which, naturally, is called PagedCollectionView. You should definitely take a look at its source code in case you are interested in what is going on under the hood. But this is not a prerequisite for our discussion.

The WPF default implementation of the interface is Telerik’s QueryableCollectionView which, among many other interfaces, implements IPagedCollectionView.

II. No Paging

NoPaging

In order to gradually build up my case, I will start with a very simple example that lacks paging whatsoever. It might sound stupid, but this will help us build on top of this paging-devoid example. Let us imagine that we have the simplest possible scenario. That is a simple IEnumerable and an ItemsControl that shows its contents. This will look like this:

No Paging
  1. IEnumerable itemsSource = Enumerable.Range(0, 1000);
  2. this.itemsControl.ItemsSource = itemsSource;

XAML
  1. <Border Grid.Row="0" BorderBrush="Black" BorderThickness="1" Margin="5">
  2.     <ListBox Name="itemsControl"/>
  3. </Border>
  4. <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1" Margin="5">
  5.     <TextBlock Text="No Paging"/>
  6. </Border>

Nothing special for now. Just some data displayed in a ListBox.

The two sample projects in the solution that I have attached are:

  • NoPaging_WPF
  • NoPaging_SL3

With every next sample those two project will evolve in some way or another.

III. Paging simple collections

The single most important property of RadDataPager is its Source property. This is where you pass in your collection of data for paging.

More often than not your collection will not be an IPagedCollectionView. It will either be a simple List<T>, or an ObservableCollection<T>, or anything that is simply IEnumerable. Unless you had paging in mind when you designed your project, it is almost certain that your data source will not be pageable out of the box. So what are the options?

III. 1. Wrapping the simple collection in an IPagedCollectionView

Wrapping

If you look at the constructors of PagedCollectionView and QueryableCollectionView you will notice that you can pass in a simple IEnumerable as a parameter. Those two classes will wrap it and provide paging capabilities over your original data. In fact, this is what RadGridView does internally. It wraps your original collection in an QueryableCollectionView in order to easily perform many useful tasks such as filtering, sorting, and others, but in our case the most important one is paging. So let us start our series of examples with the most simplistic one. Imagine that you have a simple IEnumerable which is the source for an ItemsControl. Here is how to wrap it in order to enable paging:

Silverlight
  1. IEnumerable itemsSource = Enumerable.Range(0, 1000);
  2. var pagedSource = new PagedCollectionView(itemsSource);
  3. this.radDataPager.Source = pagedSource;
  4. this.itemsControl.ItemsSource = pagedSource;

WPF
  1. IEnumerable itemsSource = Enumerable.Range(0, 1000);
  2. var pagedSource = new QueryableCollectionView(itemsSource);
  3. this.radDataPager.Source = pagedSource;
  4. this.itemsControl.ItemsSource = pagedSource;

XAML
  1. <Border Grid.Row="0"
  2.         BorderBrush="Black"
  3.         BorderThickness="1"
  4.         Margin="5">
  5.     <ListBox Name="itemsControl"/>
  6. </Border>
  7. <Border Grid.Row="1"
  8.         BorderBrush="Black"
  9.         BorderThickness="1"
  10.         Margin="5">
  11.     <telerikGrid:RadDataPager Name="radDataPager"
  12.                               PageSize="10"
  13.                              IsTotalItemCountFixed="True"
  14.                              DisplayMode="All"/>

This will do the trick. It is quite simple, isn’t it?

The two sample projects in the solution that I have attached are:

  • PagingSimpleCollectionWithWrapping_WPF
  • PagingSimpleCollectionWithWrapping_SL3
III. 2. Binding to RadDataPager.PagedSource

PagedSource

In case you do not like this approach there is a better one. When you assign an IEnumerable as the Source of a RadDataPager it will automatically wrap it in a QueryableCollectionView and expose it through its PagedSource property. From then on, you can attach any number of ItemsControl’s to the PagedSource and they will be automatically paged. Here is how to do this entirely in XAML:

Using RadDataPager.PagedSource
  1. <Border Grid.Row="0"
  2.         BorderBrush="Black"
  3.         BorderThickness="1" Margin="5">
  4.     <ListBox Name="itemsControl"
  5.              ItemsSource="{Binding PagedSource, ElementName=radDataPager}"/>
  6. </Border>
  7. <Border Grid.Row="1"
  8.         BorderBrush="Black"
  9.         BorderThickness="1"
  10.         Margin="5">
  11.     <telerikGrid:RadDataPager Name="radDataPager"
  12.                               Source="{Binding ItemsSource}"
  13.                              PageSize="10"
  14.                              IsTotalItemCountFixed="True"
  15.                              DisplayMode="All"/>

The two sample projects in the solution that I have attached are:

  • PagingSimpleCollectionWithPagedSource_WPF
  • PagingSimpleCollectionWithPagedSource_SL3

IV. Paging collections implementing IPagedCollectionView

DomainDataSource

Those of you who are using WCF RIA Services should feel very lucky. After a quick look with Reflector or the debugger we can see that the DomainDataSource.Data property is in fact an instance of the DomainDataSourceView class. This class implements a handful of useful interfaces:

Luckily, IPagedCollectionView is among them which lets you do the whole paging in the server. So let’s do this. We will add a DomainDataSource control to our page/window and connect the items control and the pager to it. Here is how to do this:

MainPage
  1. <riaControls:DomainDataSource x:Name="invoicesDataSource"
  2.                               AutoLoad="True"
  3.                               QueryName="GetInvoicesQuery">
  4.     <riaControls:DomainDataSource.DomainContext>
  5.         <services:ChinookDomainContext/>
  6.     </riaControls:DomainDataSource.DomainContext>
  7. </riaControls:DomainDataSource>
  8. <Border Grid.Row="0"
  9.         BorderBrush="Black"
  10.         BorderThickness="1"
  11.         Margin="5">
  12.     <ListBox Name="itemsControl"
  13.              ItemsSource="{Binding Data, ElementName=invoicesDataSource}"/>
  14. </Border>
  15. <Border Grid.Row="1"
  16.         BorderBrush="Black"
  17.         BorderThickness="1"
  18.         Margin="5">
  19.     <telerikGrid:RadDataPager Name="radDataPager"
  20.                               Source="{Binding Data, ElementName=invoicesDataSource}"
  21.                              PageSize="10"
  22.                              IsTotalItemCountFixed="True"
  23.                              DisplayMode="All"/>

By the way, you can replace the ListBox from the above code snippet with any other ItemsControl. It can be RadGridView, it can be the MS DataGrid, you name it. Essentially, RadDataPager is sending paging commands to the the DomainDataSource.Data. It does not care who, what, or how many different controls are bound to this same Data property of the DomainDataSource control.

So if you would like to experiment with this, you can throw in any number of other ItemsControl’s next to the ListBox, bind them in the same manner, and all of them will be paged by our single RadDataPager.

Furthermore, you can throw in any number of RadDataPager’s and bind them to the same property. Then when you page with any one of them will automatically update all of the rest.

The whole picture is simply beautiful and we can do all of this thanks to WCF RIA Services.

The two sample projects (Silverlight only) in the solution that I have attached are:

  • PagingIPagedCollectionView
  • PagingIPagedCollectionView.Web

IV. Paging RadGridView

RadGridView

While you can replace the ListBox in any of the above examples with a RadGridView, RadGridView offers something extra. Similar to the DomainDataSource.Data property, the RadGridView.Items collection implements the IPagedCollectionView interface. So you are already thinking: Then why not bind the Source property of RadDataPager to RadGridView.Items? Well that’s exactly what you can do and you will start paging RadGridView out-of-the-box. It is as simple as that, no code-behind is involved:

MainPage
  1. <Border Grid.Row="0"
  2.         BorderBrush="Black"
  3.         BorderThickness="1" Margin="5">
  4.     <telerikGrid:RadGridView Name="radGridView"
  5.                              ItemsSource="{Binding ItemsSource}"/>
  6. </Border>
  7. <Border Grid.Row="1"
  8.         BorderBrush="Black"
  9.         BorderThickness="1"
  10.         Margin="5">
  11.     <telerikGrid:RadDataPager Name="radDataPager"
  12.                               Source="{Binding Items, ElementName=radGridView}"
  13.                              PageSize="10"
  14.                              IsTotalItemCountFixed="True"
  15.                              DisplayMode="All"/>

The two sample projects in the solution that I have attached are:

  • PagingRadGridView_SL3
  • PagingRadGridView_WPF

With this last example I think I have covered every possible paging combination. In case you would like to see an example of something that I have not covered, please let me know.

Also, make sure you check out those great online examples:

Happy Paging!

Download Full Source Code


Related Posts

Comments

Comments are disabled in preview mode.