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.
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.
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:
Nothing special for now. Just some data displayed in a ListBox.
The two sample projects in the solution that I have attached are:
With every next sample those two project will evolve in some way or another.
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?
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:
This will do the trick. It is quite simple, isn’t it?
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:
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:
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:
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:
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:
Download Full Source Code