Wednesday, September 9, 2009

Working with Silverlight 3's DataPager Control

Silverligth 3's DataPager control works great if you are working with the .Net Ria Services. The .Net Ria Services make it easy for you to retrieve data from a db and page it so it is not all retrieved at once (improving performance). But how do you use the control if you want to manually create it yourself? What if you already have your paging scheme implemented and all you wish to do is use a new pager control?

This is relatively simple to do, but it isn't the most elegant code. You will need to use a PagedCollectionView. This is what you have to set the source to in your pager in order to get it to work. In order to use this class, you will need to add the following using statement to your source file.

using System.Collections.Specialized;

Once you have that, you can create a PagedCollectionView by passing an array to the constructor. In this example, I have an integer array (pagerSource) which is filled with a bunch of 1's. You could really put whatever you want in here. You can make it any type of array too, I just chose and integer one. Once I have my array the appropriate length (length will equal the pager's # of pages), I can create the PagedCollectionView object and set that as the source of my pager. I can then assign event handlers as you would expect to see when the pageIndex changes and perform my logic accordingly.

Enjoy!

int totalPages =  .//... logic to figure out my # of pages

int[] pagerSource = new int[totalPages];
for (int i = 0; i < totalPages; i++)
{
     pagerSource[i] = 1;
}

pagedCollectionView = new PagedCollectionView(pagerSource);
myPager.Source = pagedCollectionView;

No comments:

Post a Comment