Click here to Skip to main content
6,295,667 members and growing! (14,048 online)
Email Password   helpLost your password?
Web Development » Silverlight » General License: The Code Project Open License (CPOL)

How to implement paging in Silver Light2 DataGrid

By setu_raas

How to implement paging in Silver Light2 DataGrid
C# (C# 3.0), ASP.NET
Posted:26 Apr 2008
Views:11,498
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 2.08 Rating: 2.30 out of 5
2 votes, 25.0%
1
3 votes, 37.5%
2

3

4
3 votes, 37.5%
5
grid.JPG

Introduction

Silver light2 is one of the most new and exciting technology from Microsoft. As my company has decided to develop application by using Silver Ligth2 that’s why I am exploring the silver light2 controls. When I started to work with Silver Light2 Datagrid control, I saw there is no built-in paging facility. The paging facility is extremely necessary to our application. Silver light2 is still Beta1. So I am expecting these sorts of features will be added to the release version. Nevertheless I tried to develop a user control which shows you how paging can be implemented in DataGrid.

Using the code

In order to implement paging in DataGrid, I used RowDetailsTemplate property of the DataGrid. This RowDetailsTemplate gets or sets the template that is used to display the content of the details section of rows. This property is visible at the last row of the DataGrid where I put the navigation buttons. To visible the RowDetailsTemplate property for the last row, DataGrid subscribes the PreparingRow event.

    private void grdPaging_PreparingRow(object sender, DataGridRowEventArgs e)
        {
            //_countrow is the current preparing row number, totalRowsInGrid is the total row in grid.
            //totalRowsInGrid can be equal to or less then chunk size
            if (_countrow == totalRowsInGrid)
            {
                e.Row.DetailsVisibility = Visibility.Visible;
            }
            _countrow++;
        }         

To put the navigation buttons, DataGrid subscribes the PreparingRowDetails event. Here I used a stackpanel to house all the navigation buttons.


    private void grdPaging_PreparingRowDetails(object sender, DataGridRowDetailsEventArgs e)
        {
            //Get the stackPanel from the RowDetailsTemplate
            StackPanel stk = e.DetailsElement.FindName("stk") as StackPanel;
            
            if (stk != null)
            {
                stk.Children.Clear();
                int totalpage = Convert.ToInt32(Math.Ceiling(this.TotalRow/(double)this.ChunkSize));
                for (int i = _currentPage; i <= totalpage; i++)
                {
                    Button btn = new Button();                    
                    btn.Width = 20;
                    btn.Content = i;
                    btn.Click += new RoutedEventHandler(btn_Click);
                    btn.Margin = new Thickness(3);                    
                    stk.Children.Add(btn);
                }
            }
        } 

As you will see in the source code, the rest of the statements are very typical to implement paging in DataGrid.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

setu_raas


Member

Occupation: Software Developer
Company: Adaptive Enterprise Ltd
Location: Bangladesh Bangladesh

Other popular Silverlight articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Questiona question?thank you! PinmemberJoker_jfxia18:39 1 Sep '08  
GeneralNice Article. PinmemberRazanPaul11:12 8 May '08  
GeneralMissing downloads PinadminChris Maunder14:17 26 Apr '08  
GeneralRead waht Rob wrote PinmemberMustafa Ismail Mustafa12:09 26 Apr '08  
GeneralRe: Read waht Rob wrote Pinmembersetu_raas20:49 26 Apr '08  
GeneralRe: Read waht Rob wrote PinmemberMustafa Ismail Mustafa23:23 26 Apr '08  
GeneralNeeds meat PinmemberRob Graham11:49 26 Apr '08  
GeneralRe: Needs meat Pinmembersetu_raas20:51 26 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Apr 2008
Editor:
Copyright 2008 by setu_raas
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project