How to add paging to DataGrid in Silverlight

by Admin 24. February 2010 10:37

Download Sample Project (367.89 kb)

paging in silverlight datagrid

In one of my previous posts How to use DataGrid in Silverlight I showed a very simple usage where data grid was bound to a list of products. Now let us take one more step in customizing use of this data grid. When I executed my method to get list of products from AdventureWorks database, it returned me about 1000 records. And then datagrid was bound to that list, i got a huge page with grid showing all the records. I am sure at this point you are looking for way to add some kind of paging to your data grid so that user can navigate the list easily.

Silverlight has a control named DataPager that comes in very handy to add paging functionality to any control that you use to display lists. From the name is it obvious that this control is a pager. Following XAML shows how I added paging functionality to my datagrid.


<StackPanel x:Name="ContentStackPanel" Orientation="Vertical">
 <TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}" 
     Text="Products"/>
  <data:DataPager x:Name="ProductsPager" 
    PageSize="10" 
    DisplayMode="Numeric" 
    AutoEllipsis="True" 
    HorizontalAlignment="Left" />
  <data:DataGrid x:Name="ProductsGrid">
   <data:DataGrid.Columns>
   <data:DataGridTemplateColumn>
   <data:DataGridTemplateColumn.CellTemplate>
   <DataTemplate>
   <StackPanel>
   <Button x:Name="UpdateButton" Content="Update" 
     Click="UpdateButton_Click"></Button>
   </StackPanel>
   </DataTemplate>
   </data:DataGridTemplateColumn.CellTemplate>
   </data:DataGridTemplateColumn>
   </data:DataGrid.Columns>
   </data:DataGrid>
</StackPanel>

You can read more about different ways to customize the display of datapager from the documentation. For this discussion you can see that I have set PageSize, DisplayMode, AutoEipsis and HorizontalAlignment properties of pager and the screen shot shows how it looks. I will talk more about customization of DataPager in detail in next post. For now I just want to keep it to simple use.

PageViewCollection

This is the collection object that drives the functionality for DataPager. In general you need a collection that implements IPagedViewCollection interface. No, you do not have to do any more implementation to get DataPager to work. We already have list of products obtained from previous web service call. You can simply wrap that list into PagedViewCollection object and set it as Source for DataPager object. And then set the source for DataGrid as this PagedViewCollection object and we are all set to go. Following code shows simple change I made in code from previous sample project.


void GetProductsCompleted(object sender, GetProductsCompletedEventArgs e)
{
 _products = e.Result;
 if (null != _products)
 {
  _pagedProductsView = new PagedCollectionView(_products);
  ProductsPager.Source = _pagedProductsView;
  ProductsGrid.ItemsSource = _pagedProductsView;
 }
}

Add References

PagedViewCollection is defined in System.Windows.Data assembly. So you will need to add reference to this assembly in your Silverlight project and then add using directive for the namespace in your source code to refer to the classes in this namespace and assembly.

Adding Paging to DataGrid in Silverlight is as easy as that.

Views: 14588

Tags: ,

DataGrid | Silverlight

Comments

4/6/2010 1:45:28 AM #

Tiffany Keys

That sound s good.I will try.And I am trying to search

Tiffany Keys Malaysia

4/10/2010 12:11:01 AM #

migliori bonus casinò virtuale

Silverlight  is the first browser plug-in to provide access to all system colors, allowing partially-sighted people to make changes such as high contrast color schemes for ease of readability by using familiar operating system controls.

migliori bonus casinò virtuale India

4/20/2010 1:36:56 AM #

ajax ebook

Thanks for the detailed, instructions about Silverlight. I have read many ebooks to get a clear picture as to how we can gain more and more knowledge in Silverlight.

ajax ebook United States

4/20/2010 1:37:13 AM #

ajax ebook

Thanks for the detailed, instructions about Silverlight. I have read many ebooks to get a clear picture as to how we can gain more and more knowledge in Silverlight.

ajax ebook United States

4/29/2010 9:27:15 PM #

df

hi..

df United States

5/2/2010 11:54:42 PM #

Risks of Dust Mite Allergies

I never tried to use this one in my Silver light application and adding a page in Data Grid. Such an useful article for everyone, I had a great time reading it.

Risks of Dust Mite Allergies United States

5/3/2010 2:58:59 PM #

auto insurance quotes

Thanks for the detailed, instructions about Silverlight. I have read many ebooks to get a clear picture as to how we can gain more and more knowledge in Silverlight.

auto insurance quotes United States

5/12/2010 6:03:09 AM #

short term loans

First of all thanks a lot for the useful and informative article. I have to do a little research in my university about the use of DataGrid in Silverlight so I am so glad that I have found this your post. The information which you have shared for us is really good explained and I will definitely will use it in my work. Thanks a lot one more time for the useful and detailed post and I will be waiting for more such great articles like this one from you in the nearest future.

short term loans United States

5/14/2010 9:36:40 AM #

extended auto warranty

I have to admit that I have been searching for this information for a long time. Reading this wonderful entry I have known many new things about the DataGrid, which I have not  known before. As I see all your articles are informative and full of valuable information so I will definitely bookmark your website and wait for more such great posts like this one. So huge thanks  for publishing this article here, without you I would never known about such a thing ever.

extended auto warranty United States

5/24/2010 9:29:01 PM #

Mussoorie Hotels

One of the problems I met in Silverlight 2 was paging option in viewing large data content. And to do this I googled this and found ...

Mussoorie Hotels United States

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli