Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday, 14 September 2014

Paging in GridView Asp.Net

In this Article, I am going to show you PageIndexChanging Event in GridView. Suppose, We have lot of data in table than, Displaying all the data in GridView without using paging is a Bad Idea. GridView Control provides us paginig feature by which we can break the table data into pages.

Let's Begin:

1) Add GridView control from toolbox and Set AutoGenerateColumns property to false.
2) Set AllowPaging property to True and add OnPageIndexChanging to gridview.
3) I have changed PageSize property of GridView to 5 which sets the number of records to display on a page in a GridView control. Its default value is 10 for a GridView.

Default.aspx Code:


<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" CellPadding="4" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Emp ID" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" />
                <asp:BoundField DataField="City" HeaderText="City" />
            </Columns>
            <HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>
            <RowStyle BackColor="#e7ceb6" />
        </asp:GridView>
    </div>
</form>
The PageIndexChanging event is fired when one of the pager buttons is clicked. We set GridView.PageIndex Property using NewPageIndex property of GridViewPageEventArgs.

Default.aspx.cs Code:

using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    //Connection String from web.config file.
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            DisplayData();
        }
    }
    //Method for Binding Data
    protected void DisplayData()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(cs);
        SqlDataAdapter adapt = new SqlDataAdapter("select * from Record",con);
        con.Open();
        adapt.Fill(dt);
        con.Close();
        if(dt.Rows.Count>0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    //PageIndexChanging occurs on clicking the pager buttons.
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DisplayData();
    }
}
Final Preview:
Hope you like it. Thanks.
[Download Source via Google Drive]

Watch Video:

0 comments:

Post a Comment

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook