PageIndexChanging event in Gridview in Asp.Net - ProgramIdea

PageIndex Changing Event In GridView In Asp.Net

Here we are binding a GridView in Asp.Net.

Points Of Remember:

1. Set GridView's AllowPaging Propery to True.

1. Fire GridView's PageIndexChanging event.

2. Add namespace System.Data and System.Data.SqlClient in your C# page.

<!DOCTYPE html>


<html xmlns="">

<head runat="server">

    <title>Pagination in GridView in Asp.Net</title>



    <form id="form1" runat="server">


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"

                          AllowPaging="True" CellPadding="4" ForeColor="#333333"

          OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10" Width="600px">


                <asp:BoundField DataField="Name" HeaderText="Name" />

                <asp:BoundField DataField="Branch" HeaderText="Branch" />

                <asp:BoundField DataField="City" HeaderText="City" />


        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />        

        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />






C# Codes :

using System.Data;

using System.Data.SqlClient;


public partial class GridView_PageIndexChanging : System.Web.UI.Page


    // sql connection

    SqlConnection con = new SqlConnection( @"Data Source=JITESH-PC\SQL;Initial

                                                                                              Catalog=db_Student;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)


        if (!IsPostBack)





    //method for binding GridView

    protected void BindGridView()


        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter( "Select ID, Name,Branch,City from tbl_student" , con);





        if (dt.Rows.Count > 0)


            GridView1.DataSource = dt;




    // gridview pageindex changing event

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)


        GridView1.PageIndex = e.NewPageIndex;