Tuesday, 27 January 2015

Page Count Display in GridView Footer In Asp.Net Using C#.Net

1/27/2015 - By Pranav Singh 0

This article will show you how you can Page Count Display in GridView Footer In Asp.Net Using C#.Net. In this we will display total no of pages in gridview and total page visited by user.


So for this article first we will create a new asp.net application and add the below code in the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page Count Display in GridView Footer In Asp.Net Using C#.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="4" cellspacing="4">
        <tr>
            <td align="left">
                <asp:GridView ID="GridView1" runat="server" Width="100%"
                    AutoGenerateColumns="False" AllowPaging="True"
                    onpageindexchanging="GridView1_PageIndexChanging" PageSize="3"
                    onrowdatabound="GridView1_RowDataBound" ShowFooter="True">
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" />
                        <asp:BoundField DataField="Address" HeaderText="Address" />
                        <asp:BoundField DataField="UserType" HeaderText="UserType" />
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>   
    </form>
</body>
</html>

In above code I have use BoundField for bind the user detail.
Now check the code to bind and display no of pages.

using System;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView();
            }
        }
        private void BindGridView()
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = "select * from UserDetail;";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }

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

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[2].Text ="Page :"+ (GridView1.PageIndex + 1) + " of " + GridView1.PageCount;
            }
        }
    }
}

On above code I have used a function to bind the data. Now I have I have added the paging in gridview to display the pager. Now check the below code to display the no of pages.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[2].Text ="Page :"+ (GridView1.PageIndex + 1) + " of " + GridView1.PageCount;
            }
        }

In above code total no of pages and pages visited by user have been displayed in the cell of the pages.
Now we have done run the application to check the output.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top