Click here to Skip to main content
6,595,854 members and growing! (19,553 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » Grid controls     Intermediate License: The Code Project Open License (CPOL)

GridView Custom Paging with PageSize Change Dropdown

By Saifi Hasan

A GridView control with custom paging.
C# (C# 2.0), Win Mobile, .NET (.NET 2.0), ASP.NET, WebForms, Architect
Posted:5 May 2008
Views:50,900
Bookmarked:68 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
27 votes for this article.
Popularity: 6.58 Rating: 4.60 out of 5
1 vote, 3.7%
1

2
2 votes, 7.4%
3
3 votes, 11.1%
4
21 votes, 77.8%
5

GridView Custom paging with PageSize change dropdown

Introduction

The GridView is a very powerful control and has all the features including paging/sorting etc. But while using GridView in your project, you have to assign the PageSize property of the GridView programmatically, which users then cannot change. Sometimes, users require the feature to change the page size so that they could view more/less records per page. With one of my projects, I was asked to develop such a control with custom paging. I am sharing this with you so that you don't have to rework for such a control.

Using the code

The complete code and assemblies are attached with this article. You can download the MrllControlLib.dll and add a reference to it in your project. Once you add the control to your page, you can assign various properties in order to control the attribute set in the context menu. The following are some of the properties exposed:

  • ShowMrllCustomPaging boolean
  • CustomPagerCssClass
  • DefaultSortExpression

Note that if you have assigned ShowMrllCustomPaging=true, then the control will load all drop downs for paging, sorting (if the SortExpression is assigned) and the number of pages dropdown, otherwise it will work as a normal GridView. Also, make sure to handle all events like PageIndexChanging and OnSorting. I have added another event, OnPageSizeChanged, which is fired when the page-size drop down is changed. This event uses the PageSizeChangeEventArgs delegate and emits a new page-size value. The following is the implementation of the PageSizeChange event:

protected void gv1_PageSizeChange(object sender, PageSizeChangeEventArgs e) 
{ 
    gv1.PageSize= e.NewPageSize; 
    gv1.PageIndex = 0; 
    BindGrid(); 
}

The rest of the events like OnSorting/PageIndexChanging are the same as for the GridView.

About the code

The custom gridview control inherits from the .NET GridView control, and the OnRowCreated method is overriden in order to change the paging style. When there is more than one page, the default pager template is loaded. I clear all the controls in the pager template and add my controls for paging/sorting/the number of records drop down.

protectedd override OnRowCreated(GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Pager) 
    { 
          e.Row.Cells[0].Controls.Clear(); 
          //Code to Add various Dropdown 
          //Add control to Row 
          // oOutertab is table containg all dropdown and Lable 
          
          e.Row.Cells[0].Controls.Add(oOutertab); 
    }
}

Now, the problem is when there is only one page, the PagerTemplate will not be created, so how can I put the custom paging and sorting dropdown? The answer is add a row to the GridView at position zero (just above the header row). I have done this by adding a row when the header is created.

protected override void OnRowCreated(GridViewRowEventArgs e)
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
        if (PageCount ==1) 
        { 
            GridViewRow PagerRow = base.CreateRow(-1, -1, 
               DataControlRowType.Pager, DataControlRowState.Normal); 
            //Add Your container of controls in this New row 
            PagerRow.Cells.Add(new TableCell()); 
            PagerRow.Cells[0].Controls.Add(oTab);  
            PagerRow.Cells[0].ColumnSpan = this.Columns.Count; 
 
            //Add the New row at Position 0 of GrdView 
            Table grid = (Table)this.Controls[0]; 
                                grid.Rows.AddAt(0, PagerRow); 
        } 
    } 
}

Now, there is another issue. Once you add a row at position zero, then it will have the style of the header-template, since we added this row while the header row was getting created. So, to apply the correct styling on the correct row, we need to assign CustomPagerCssClass to the pager template and HeaderStyleCssClass to the Header template (which is at position 1 now), or we can assign CSS on the RowCreated event before adding a new row at position zero.

GridViewRow PagerRow = base.CreateRow(-1, -1, 
    DataControlRowType.Pager, DataControlRowState.Normal); 
if (this.CustomPagerCssClass != string.Empty) 
      PagerRow.CssClass = this.CustomPagerCssClass; 
 
if (this.HeaderStyle.CssClass != string.Empty) 
      e.Row.CssClass = this.HeaderStyle.CssClass; 
//Now Add Pager Row at Position zero 
Table grid = (Table)this.Controls[0]; 
grid.Rows.AddAt(0, PagerRow);

License

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

About the Author

Saifi Hasan


Member
I have done Master Degree in Computers and MCAD and working on Microsoft technologie since last 4 yrs. Currently working with TCS (India).
Location: India India

Other popular Grid & Data Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralGood article but it should have little improvemets Pinmemberfellippe1:47 31 Mar '09  
Generalcan this be used in a form? Pinmembermark chester goking20:51 13 Nov '08  
GeneralVery NICE! Thank You! PinmemberMember 31061884:04 8 Nov '08  
GeneralGood work PinmemberTittle Joseph1:24 31 Jul '08  
GeneralRe: Good work PinmemberSaifi Hasan13:33 1 Aug '08  
GeneralThere was an error rendering the control. PinmemberMaclir10:04 30 Jul '08  
Generalcan not run the demo PinmemberMember 1041997:20 21 Jul '08  
GeneralRe: can not run the demo PinmemberMember 10419910:29 21 Jul '08  
GeneralRe: can not run the demo PinmemberSaifi Hasan14:05 24 Jul '08  
GeneralCannot find column DESC Pinmemberwissam120:37 16 Jul '08  
GeneralRe: Cannot find column DESC [modified] PinmemberMember 310618819:16 7 Nov '08  
GeneralSaifi great job Pinmemberwasimsharp1:04 5 Jun '08  
Generalgreat job PinmemberMember 392073620:41 7 May '08  
GeneralRe: great job PinmemberSaifi Hasan3:38 9 May '08  
GeneralRe: great job PinmemberSaifi Hasan14:09 24 Jul '08  
GeneralHi Friend Pinmemberrao_2115:40 7 May '08  
GeneralRe: Hi Friend PinmemberSaifi Hasan1:40 9 May '08  
GeneralHi Buddy Pinmemberrao_2113:57 7 May '08  
GeneralGud work buddy PinmemberHemJoshi2:43 6 May '08  
Generalvery useful article PinmemberVivek_Bhat1:01 6 May '08  
GeneralGood Pinmemberkousarrabiya21:03 5 May '08  
GeneralVery Good inovation PinmemberAbhijit Jana20:52 5 May '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 5 May 2008
Editor: Smitha Vijayan
Copyright 2008 by Saifi Hasan
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project