Paging for First, Next, Previous and Last in gridview

Syedshakeer
Posted by in ASP.NET category on for Intermediate level | Views : 59298 red flag
Rating: 5 out of 5  
 1 vote(s)

From this Article you can know how make Paging for First, Next, Previous and Last in GridView.

1) First  fill the gridview with Data from a Table.

2) Drag and Drop 4 Buttons keep it at the bottom of the Gridview.

3) Change the text of a buttons using Properties.

4) Select Gridview Properties and set AllowPaging=True. See the below picture

 

 

 

5) Expand the PageSetting Properties of Gridview, and set visible property as False.

6) Next we have to set a gridview PageSize property. The Default

   PageSize is 10.you can keep how  much size you want to display rows per page.

   Here I kept 6 rows to display in gridview per page. You can observe how to set

  page in ‘Pagesetting Picture’ marked with yellow color...

 

 

7) Under SelectedRowStyle set ‘ShowFooter as False’

8) The Ouptput 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 ‘First’ Button and write the below code.

 

     protected void btnfirst_Click(object sender, EventArgs e)

        {

        gridview1.PageIndex = 0;

     }

 

 

Explantion: when you run the Page it contains 6 recors in Gridview.

Suppose the user in last Page.When the user clicks  on ‘First Button’ first 6 records of the page has to be display in 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 ‘Next Button ‘and write the below code.

 

   protected void btnnext_Click(object sender, EventArgs e)

    {

         int i = gridview1.PageIndex + 1;

            if (i <= gridview1.PageCount)

            {

                gridview1.PageIndex = i;

         }

}

Explanation: ’PageCount’ is used to count number of pages availble in a Gridview.

 

3)     Paging for Previous Button: Double click on ‘Previous Button‘and write the below code.

 

       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 ‘Last Button‘and write the below code/

 

   protected void btnlast_Click(object sender, EventArgs e)

    {

      

            gridview1.PageIndex = gridview1.PageCount;

}

 

 

In the Above Picture observe on Last Button.it is in Enable=False state,because in gridview the last page records are displaying.So again no need with last Button.In same Senario remaining buttons also made Enable=False where the buttons are not usefull to click the user.you can see the coding fo Enable in .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 as Custom Paging.

Page copy protected against web site content infringement by Copyscape

About the Author

Syedshakeer
Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
http://www.dotnetfunda.com
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)