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.

  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 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...

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


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;

    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;