Tuesday, April 17, 2012

Paging for First, Next, Previous and Last outside gridview with Buttons

From this post we can know how to make Paging for First, Next, Previous and Last for GridView.
·         Write a method to bind gridview manually.
·         Drag and drop 4 imagebuttons below gridview and change the button id’s accordingly.
·         In the grodview properties select allow paging = true as shown in the figure




·         Expand pagersettings and select visible = false as shown



·         Now change the page size to 9.
·         Change gridview autoformat as you like, now our gridview looks as shown below.



  Now doubleclick on each imagebutton to generate clickevents.
  In the first imagebutton click evet write below code

    protected void imgBtnFirst_Click(object sender, ImageClickEventArgs e)
    {
   GridView1.PageIndex = 0;
   bindGridview();
         imgBtnFirst.Enabled = false;
         imgBtnNext.Enabled = true;
         imgBtnPrev.Enabled = false;
   imgBtnLast.Enabled = true;
    }

And finally the entire coding looks as shown below

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGridview();
            imgBtnFirst.Enabled = false;
            imgBtnPrev.Enabled = false;
        }
    }

    private void bindGridview()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("SNO");
        dt.Columns.Add("Name");
        dt.Columns.Add("Class");
        dt.Columns.Add("Rank");
        DataRow dr;
        for (int i = 0; i < 56; i++)
        {
            dr = dt.NewRow();
            dr["SNO"] = (i + 1).ToString();
            dr["Name"] = "Sample Name " + (i + 1).ToString();
            dr["Class"] = "Sample Class " + (i + 1).ToString();
            dr["Rank"] = "Sample Rank " + (i + 2).ToString();
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void imgBtnFirst_Click(object sender, ImageClickEventArgs e)
    {
        GridView1.PageIndex = 0;
        bindGridview();
        imgBtnFirst.Enabled = false;
        imgBtnNext.Enabled = true;
        imgBtnPrev.Enabled = false;
        imgBtnLast.Enabled = true;
    }
    protected void imgBtnNext_Click(object sender, ImageClickEventArgs e)
    {
        int i = GridView1.PageIndex + 1;       
        if (i <= GridView1.PageCount)
            GridView1.PageIndex = i;
        bindGridview();
        imgBtnFirst.Enabled = true;
        imgBtnNext.Enabled = true;
        imgBtnPrev.Enabled = true;
        imgBtnLast.Enabled = true;
    }
    protected void imgBtnPrev_Click1(object sender, ImageClickEventArgs e)
    {
        int i = GridView1.PageCount;       
        if (GridView1.PageIndex > 0)
            GridView1.PageIndex = GridView1.PageIndex - 1;
        if (GridView1.PageIndex == 0)
        {
            imgBtnFirst.Enabled = false;
            imgBtnPrev.Enabled = false;
        }
        else
        {
            imgBtnFirst.Enabled = true;
            imgBtnPrev.Enabled = true;
        }
        bindGridview();       
        imgBtnNext.Enabled = true;       
        imgBtnLast.Enabled = true;
    }
    protected void imgBtnLast_Click1(object sender, ImageClickEventArgs e)
    {      
        GridView1.PageIndex = GridView1.PageCount;
        bindGridview();
        imgBtnFirst.Enabled = true;
        imgBtnNext.Enabled = false;
        imgBtnPrev.Enabled = true;
        imgBtnLast.Enabled = false;
    }

}

Download from Here

1 comment: