In this article I will explain with an example, how to solve the following exception (error) that occurs while implanting paging in ASP.Net GridView control.
        
        
            The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
        
        ![[Solution] The GridView 'GridView1' fired event PageIndexChanging which wasn't handled]() 
        
             
        
        
             
        
        
            Cause
        
        
            The above error occurs when you set AllowPaging property to True and do not specify the PageIndexChanging event hander for the GridView.
        
        
            The PageIndexChanging event handler handles the GridView paging process and it is very important to specify it whenever you implement paging in GridView control.
        
        
            In the following article, I will explain the correct steps to implement paging in ASP.Net GridView control.
        
        
            In this article I will explain how to implement Paging (Pagination) in GridView control in ASP.Net using C# and VB.Net.
        
        
             
        
        
             
        
        
            Database
        
        
            Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
        
        
        
             
        
        
             
        
        
            HTML Markup
        
        
            The following HTML Markup consists of an ASP.Net GridView with four BoundField columns. In order to implement Paging in GridView, AllowPaging property is set to true and OnPageIndexChanging event has been handled.
        
        
            
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
            
            
                    OnPageIndexChanging="OnPageIndexChanging" PageSize="10">
            
            
                    <Columns>
            
            
                        <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="Customer ID" />
            
            
                        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
            
            
                        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
            
            
                        <asp:BoundField ItemStyle-Width="150px" DataField="Country" HeaderText="Country" />
            
            
                    </Columns>
            
            
                </asp:GridView>
            
         
        
             
        
        
             
        
        
            Namespaces
        
        
            You will need to import the following namespaces.
        
        
            C#
        
        
            
                using System.Data;
            
            
                using System.Data.SqlClient;
            
            
                using System.Configuration;
            
         
        
             
        
        
            VB.Net
        
        
            
                Imports System.Data
            
            
                Imports System.Data.SqlClient
            
            
                Imports System.Configuration
            
         
        
             
        
        
             
        
        
            Populating GridView from database
        
        
            Inside the Page Load event of the page, the GridView is populated with records from the Customers table of the Northwind database.
        
        
            C#
        
        
            
                protected void Page_Load(object sender, EventArgs e)
            
            
                {
            
            
                    if (!this.IsPostBack)
            
            
                    {
            
            
                        this.BindGrid();
            
            
                    }
            
            
                }
            
            
                 
            
            
                private void BindGrid()
            
            
                {
            
            
                    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            
            
                    using (SqlConnection con = new SqlConnection(constr))
            
            
                    {
            
            
                        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, ContactName, City, Country FROM Customers"))
            
            
                        {
            
            
                            using (SqlDataAdapter sda = new SqlDataAdapter())
            
            
                            {
            
            
                                cmd.Connection = con;
            
            
                                sda.SelectCommand = cmd;
            
            
                                using (DataTable dt = new DataTable())
            
            
                                {
            
            
                                    sda.Fill(dt);
            
            
                                    GridView1.DataSource = dt;
            
            
                                    GridView1.DataBind();
            
            
                                }
            
            
                            }
            
            
                        }
            
            
                    }
            
            
                }
            
         
        
             
        
        
            VB.Net
        
        
            
                Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            
            
                    If Not Me.IsPostBack Then
            
            
                        Me.BindGrid()
            
            
                    End If
            
            
                End Sub
            
            
                 
            
            
                Private Sub BindGrid()
            
            
                    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            
            
                    Using con As New SqlConnection(constr)
            
            
                        Using cmd As New SqlCommand("SELECT CustomerId, ContactName, City, Country FROM Customers")
            
            
                            Using sda As New SqlDataAdapter()
            
            
                                cmd.Connection = con
            
            
                                sda.SelectCommand = cmd
            
            
                                Using dt As New DataTable()
            
            
                                    sda.Fill(dt)
            
            
                                    GridView1.DataSource = dt
            
            
                                    GridView1.DataBind()
            
            
                                End Using
            
            
                            End Using
            
            
                        End Using
            
            
                    End Using
            
            
                End Sub
            
         
        
             
        
        
             
        
        
            Implementing Paging in GridView
        
        
            The following event handler is executed when a page is changed inside the GridView. 
        
        
            The value of the PageIndex of the Page which was clicked is present inside the NewPageIndex property of the GridViewPageEventArgs object and it is set to the PageIndex property of the GridView and the GridView is again populated by calling the BindGrid function.
        
        
            C#
        
        
            
                protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
            
            
                {
            
            
                    GridView1.PageIndex = e.NewPageIndex;
            
            
                    this.BindGrid();
            
            
                }
            
         
        
             
        
        
            VB.Net
        
        
            
                Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
            
            
                    GridView1.PageIndex = e.NewPageIndex
            
            
                    Me.BindGrid()
            
            
                End Sub
            
         
        
             
        
        
             
        
        
            Screenshot
        
        ![[Solution] The GridView 'GridView1' fired event PageIndexChanging which wasn't handled]() 
        
             
        
        
             
        
        
            Demo
        
        
        
             
        
        
             
        
        
            Downloads