Go to DotNetFunda.com
 Online : 2779 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Paging for First, Next, Previous and Last in gridview

  • Nominate yourself for "Agile Software Development using Scrum" online session for FREE, click here.

  • Download OOPS and ASP.NET Online training session video and PPT from here.

Submit Article | Articles Home | Search Articles |

Paging for First, Next, Previous and Last in gridview

 Posted on: 8/16/2009 12:47:43 PM by Syedshakeer | Views: 1877 | Category: ASP.NET | Level: Intermediate | Print Article
From this Article you can know how make Paging for First, Next, Previous and Last in GridView.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.


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.


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as Software Developer on .NET and MVM of www.dotnetspider.com
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)