Paging for First, Next, Previous and Last in gridview



This Article explains how to do Paging for First, Next, Previous and Last in A GridView.

  1. First fill the GridView with data from a table.
  2. Drag and Drop 4 Buttons below the GridView.
  3. Change the text of the buttons using Properties.
  4. Select Gridview Properties and set AllowPaging=True. See the following picture.

    paging1.gif
     
  5. Expand the Page Setting Properties of GridView, and set the visible property to False.
  6. Next we have to set the GridView PageSize property. The default PageSize is 10.
    You can keep the default value for the number of rows per page to display.
    Here I kept 6 rows to display in the GridView per page. You can observe how to set
    the Page in 'Pagesetting Picture' marked with the color yellow...

    paging2.gif
     
  7. Under SelectedRowStyle set 'ShowFooter as False'
  8. The Output of your GridView Image is shown below.

    paging3.gif

How to Write the Code for Paging First, Next, Previous and Last.
  1. Paging for First Button: Double click on the "First" Button and use the following code for it.

    protected void btnfirst_Click(object sender, EventArgs e)
    {
       gridview1.PageIndex = 0;
    }

    paging4.gif

    Explanation: when you run the page it contains 6 records in GridView.

    Suppose the user is in the last page. When the user clicks on the "First Button" the first 6 records of the page are displayed in the GridView. So the first GridView page Index(property) is "zero". There is a Property called "PageIndex" for GridView.
     
  2. Paging for Next Button: Double click on the "Next Button" and use the following code for it.
     

    protected void btnnext_Click(object sender, EventArgs e)

    {

         int i = gridview1.PageIndex + 1;

         if (i <= gridview1.PageCount)

         {

              gridview1.PageIndex = i;
    }


    paging5.gif

    Explanation: 'PageCount' is used to count number of pages available in a Gridview.
     

  3. Paging for Previous Button: Double click on the "Previous Button" and use the following code for it.
     

       protected void btnprevious_Click(object sender, EventArgs e)
     {

            int i = gridview1.PageCount;

             if (gridview1.PageIndex > 0)

            {

              gridview1.PageIndex = gridview1.PageIndex - 1;

                   

             }

         }

     

  4. Paging for Last Button: Double click on the "Last Button" and use the following code for it.
     

       protected void btnlast_Click(object sender, EventArgs e)
       {

               gridview1.PageIndex = gridview1.PageCount;

    }

    paging6.gif


In the picture above observe the Last Button. It is in the Enable=False (disabled) state, because in GridView the last page records are already being displayed. Therefore the Last Button is not needed. Corespondingly the other buttons are also disabled when not relevant. You can see the coding for the Enable property in the .aspx.cs page.

The Complete .aspx.cs Page

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {

        btnfirst.Enabled = false;
        btnprevious.Enabled = 
false;
    }
protected void btnnext_Click(object sender, EventArgs e)
    {

            int i = gridview1.PageIndex + 1;
            
if (i <= gridview1.PageCount)
            {
                gridview1.PageIndex = i;
                btnlast.Enabled = 
true;
                btnprevious.Enabled = 
true;
                btnfirst.Enabled = 
true;
            }

            if (gridview1.PageCount-1 == gridview1.PageIndex)
            {
                btnnext.Enabled = 
false;
                btnlast.Enabled = 
false;
            }          
    }
    
protected void btnprevious_Click(object sender, EventArgs e)
    {    
            
int i = gridview1.PageCount;
            
if (gridview1.PageIndex > 0)
            {

                gridview1.PageIndex = gridview1.PageIndex - 1;
                btnlast.Enabled = 
true ;
            }

            if (gridview1.PageIndex == 0)
            {
                btnfirst.Enabled = 
false;
            }
            
if (gridview1.PageCount - 1 == gridview1.PageIndex+1)
            {
                btnnext.Enabled = 
true;
            }
            
if (gridview1.PageIndex == 0)
            {
                btnprevious.Enabled = 
false;
            }
    }
    
protected void btnlast_Click(object sender, EventArgs e)
    {
            gridview1.PageIndex = gridview1.PageCount;
            btnlast.Enabled = 
false;
            btnfirst.Enabled = 
true;
    }
    
protected void btnfirst_Click(object sender, EventArgs e)
    {
        gridview1.PageIndex = 0;
        btnfirst.Enabled = 
false;
        btnprevious.Enabled = 
false;
        btnlast.Enabled = 
true;
        btnnext.Enabled = 
true;

    }
}
 


This type of Paging is also called Custom Paging.

  

erver'>
Up Next
    Ebook Download
    View all
    Learn
    View all