Home > ASP.Net > Custom Paging with Gridview

Custom Paging with Gridview


Hi

try this for custom paging with Gridview

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" CellSpacing="2" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"
            OnDataBound="GridView1_DataBound" OnPageIndexChanging="GridView1_PageIndexChanging">
            <PagerSettings FirstPageImageUrl="~/Image/i_p_firstpg.gif" LastPageImageUrl="~/Image/i_p_lastpg.gif"
                Mode="NextPreviousFirstLast" NextPageImageUrl="~/Image/i_p_nextpg.gif" PreviousPageImageUrl="~/Image/i_p_prevpg.gif" />
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <Columns>
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
            </Columns>
            <PagerTemplate>
                <asp:ImageButton ID="btnFirst" runat="server" CommandArgument="First" CommandName="Page"
                    ImageUrl="~/Image/i_p_firstpg.gif" /> <asp:ImageButton ID="btnPrev" runat="server"
                        CommandArgument="Prev" CommandName="Page" ImageUrl="~/Image/i_p_prevpg.gif" /> <asp: DropDownList
                            ID="DDLPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDLPage_SelectedIndexChanged"
                            Width="44px">
                        </asp: DropDownList>  <asp:Label ID="lblCurrent" runat="server"></asp:Label>
                of
                <asp:Label ID="lblPages" runat="server"></asp:Label> Pages
                <asp:ImageButton ID="btnNext"
                    runat="server" CommandArgument="Next" CommandName="Page" ImageUrl="~/Image/i_p_nextpg.gif" /> <asp:ImageButton
                        ID="btnLast" runat="server" CommandArgument="Last" CommandName="Page" ImageUrl="~/Image/i_p_lastpg.gif" />
            </PagerTemplate>
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]">
        </asp:SqlDataSource>

and in code behind :-


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default7 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.BottomPagerRow;

        if (row == null)
        {
            return;
        }

        DropDownList DDLPage = (DropDownList)row.Cells[0].FindControl("DDLPage");
        Label lblPages = (Label)row.Cells[0].FindControl("lblPages");
        Label lblCurrent = (Label)row.Cells[0].FindControl("lblCurrent");

        //if (lblPages != null)
        //{
            lblPages.Text = GridView1.PageCount.ToString();
        //}

        //if (lblCurrent != null)
        //{
            int currentPage = GridView1.PageIndex + 1;
            lblCurrent.Text = currentPage.ToString();
        //}

        if (DDLPage != null)
        {
            for (int i = 0; i < GridView1.PageCount; i++)
            {
                int pageNumber = i + 1;
                ListItem item = new ListItem(pageNumber.ToString());
                if (i == GridView1.PageIndex)
                {
                    item.Selected = true;
                }
                DDLPage.Items.Add(item);
            }
        }

        //-- For First and Previous ImageButton
        if (GridView1.PageIndex == 0)
        {
            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnFirst")).Enabled = false;
            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnFirst")).Visible = false;

            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnPrev")).Enabled = false;
            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnPrev")).Visible = false;

            //--- OR ---\\
            //ImageButton btnFirst = (ImageButton)row.Cells[0].FindControl("btnFirst");
            //ImageButton btnPrev = (ImageButton)row.Cells[0].FindControl("btnPrev");
            //btnFirst.Visible = false;
            //btnPrev.Visible = false;

        }

        //-- For Last and Next ImageButton
        if (GridView1.PageIndex + 1 == GridView1.PageCount)
        {
            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnLast")).Enabled = false;
            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnLast")).Visible = false;

            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnNext")).Enabled = false;
            ((ImageButton)GridView1.BottomPagerRow.FindControl("btnNext")).Visible = false;

            //--- OR ---\\
            //ImageButton btnLast = (ImageButton)row.Cells[0].FindControl("btnLast");
            //ImageButton btnNext = (ImageButton)row.Cells[0].FindControl("btnNext");
            //btnLast.Visible = false;
            //btnNext.Visible = false;
        }

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void DDLPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row=GridView1.BottomPagerRow;
        DropDownList DDLPage = (DropDownList)row.Cells[0].FindControl("DDLPage");
        GridView1.PageIndex = DDLPage.SelectedIndex;
        GridView1.DataBind();
    }

}

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: