Click here to Skip to main content
Click here to Skip to main content

The easiest way to implement NumericNextPrev paging of GridView

, 21 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to get > in your GridView pager in one step. One C# method. No code download needed.


GridView has no built-in pager mode that allows you to display both next/previous page links as well as page numbers. I have no idea why. But it's really easy to implement.


Once I had requirement to do this, I've found several solutions over the network but all of them were to complex. Creating several files, catching bugs, injecting code at several points of control flow... uff, why so difficult? GridView control has already the possibility to display page numbers and ALL that I need was just add "<<" and ">>" buttons! If this is your case feel free to use my code.

Using the code 

0. Implement common paging using "Numeric" type of pager. My code expects pager in the bottom position but it's easy to make changes if you want to use it on top. 

1. Your GridView should bind the "DataBound" event: OnDataBound="gv_DataBound"

2. Use this code:  

protected void gv_DataBound(object sender, EventArgs e)

private static void EnableNextPrevNavigationForNumericPagedGrid(GridView gv)
    if (gv.BottomPagerRow == null)
    Table pagerTable = (Table)gv.BottomPagerRow.Controls[0].Controls[0];

    bool prevAdded = false;
    if (gv.PageIndex != 0)
        TableCell prevCell = new TableCell();
        LinkButton prevLink = new LinkButton
                                  Text = "<<",
                                CommandName = "Page",
                                CommandArgument = ((LinkButton)pagerTable.Rows[0].Cells[gv.PageIndex - 1].Controls[0]).CommandArgument
        pagerTable.Rows[0].Cells.AddAt(0, prevCell);
        prevAdded = true;

    if (gv.PageIndex != gv.PageCount - 1)
        TableCell nextCell = new TableCell();
        LinkButton nextLink = new LinkButton
                                  Text = ">>",
                                CommandName = "Page",
                                CommandArgument = ((LinkButton)pagerTable.Rows[0].Cells[gv.PageIndex + 
                                  (prevAdded ? 2 : 1)].Controls[0]).CommandArgument

Paging works in normal way. The only thing that my code does is adding "<<" and ">>" buttons. "<<" button won't appear at the first page as well as ">>" at the last one.  


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


About the Author

Web Developer GrapeCity
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

AnswerFixed Code PinmemberSlimSjakie15-Oct-14 0:15 
Questionnext and previous links disappear PinmemberRajarya_2229-Jul-14 10:24 
QuestionRequire one information Pinmemberrmad199021-Jun-13 7:05 
QuestionGetting Error PinmemberMember 1004213910-May-13 0:38 
GeneralMy vote of 5 Pinmemberliltium17-Oct-12 22:52 
GeneralMy vote of 3 PinmemberDrABELL21-Sep-12 6:38 
GeneralRe: My vote of 3 Pinmembertogoa21-Sep-12 6:52 
GeneralRe: My vote of 3 PinmemberDrABELL21-Sep-12 7:04 
GeneralRe: My vote of 3 Pinmembertogoa23-Sep-12 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 21 Sep 2012
Article Copyright 2012 by togoa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid