Search code examples
c#asp.netgridviewnested-gridview

Nested GridViews in ASP.Net using C# problem with change page


I am trying to open my nested gridview for a single Parent Row and gets correctly open with one single record of the child row.

The paging of the rows is provided in the main gridview.

My problem is the page change, when I try change page I have this error :

GridView paging event gvProducts_PageIndexChanging not firing

How to do resolve this ?

My code below.

Thanks in advance for any help.

Code Markup

<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false" CssClass="Grid"
    DataKeyNames="CustomerID" OnRowDataBound="gvProducts_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <img alt = "" style="cursor: pointer" src="images/plus.png" />
                <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
    </Columns>

    <PagerTemplate>
       <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/aspnet/img/bot_back_doppio.gif"
                        CommandArgument="First" CommandName="Page" />
        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/aspnet/img/bot_back.gif"
                        CommandArgument="Prev" CommandName="Page" />
         Page
         <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
                            OnSelectedIndexChanged="ddlPages_SelectedIndexChanged">
         </asp:DropDownList>
          of
         <asp:Label ID="lblPageCount" runat="server"></asp:Label>
         <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="/aspnet/img/bot_next.gif"
                        CommandArgument="Next" CommandName="Page" />
         <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/aspnet/img/bot_next_doppio.gif"
                        CommandArgument="Last" CommandName="Page" />
    </PagerTemplate>

</asp:GridView>

Code Behind

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string customerId = gvProducts.DataKeys[e.Row.RowIndex].Value.ToString();

        sql = @String.Format(" SELECT ... ");

        GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
        gvOrders.DataSource = GetData(sql);
        gvOrders.DataBind();
    }


    if (e.Row.RowType == DataControlRowType.Pager)
    {
        DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
        Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");

        if (lblPageCount != null)
            lblPageCount.Text = gvProducts.PageCount.ToString();

        for (int i = 1; i <= gvProducts.PageCount; i++)
        {
            ddl.Items.Add(i.ToString());
        }

        ddl.SelectedIndex = gvProducts.PageIndex;

        if (gvProducts.PageIndex == 0)
        {
            ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
            ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
        }

        if (gvProducts.PageIndex + 1 == gvProducts.PageCount)
        {
            ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
            ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
        }
    }
}


protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gvrPager = gvProducts.BottomPagerRow;
    DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
    gvProducts.PageIndex = ddlPages.SelectedIndex;
    BindData();
}

protected void Paginate(object sender, CommandEventArgs e)
{
    int intCurIndex = gvProducts.PageIndex;

    switch (e.CommandArgument.ToString().ToLower())
    {
        case "First":
            gvProducts.PageIndex = 0;
            break;
        case "Prev":
            gvProducts.PageIndex = intCurIndex - 1;
            break;
        case "Next":
            gvProducts.PageIndex = intCurIndex + 1;
            break;
        case "Last":
            gvProducts.PageIndex = gvProducts.PageCount - 1;
            break;
    }
    gvProducts.DataBind();
}

protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvProducts.PageIndex = e.NewPageIndex;
    BindData();
}

Solution

  • Please try this :

    [Solution] The GridView 'GridView1' fired event PageIndexChanging which wasn't handled

    I hope I was helpful.