locked
Unable to find nested gridview control RRS feed

  • Question

  • User1546632044 posted

    Hi Forum,

    Below is the issue replication steps:-

    I have nested grid view, where the web page has the gridview(parent and child) added in the designer itself.
    Columns are being added dynamically in both parent and child gridviews
    Data is getting binded properly in both parent and child gridviews on page load.
    After applying certain filter criterias I am clearing the datasource from the parent grid and rebinding the data again, where in the RowDataBound event I am unable to find the nested gridview control in the gridview row.
    Could some one help me in this case, so that I can rebind the data again

    //---------------------------------------------------------------------------------------------------------
    //Designer Code
    //---------------------------------------------------------------------------------------------------------
                            <%--START ECR Level--%>
                              <div class="body_table ">
                         <div class="body_table ">
                      <asp:UpdatePanel ID="UpdpnlgvECRLevel" UpdateMode="Conditional" runat="server">
                        <ContentTemplate>
                        
                              <asp:GridView ID="gvECRLevel" SkinID="GridView-NoBackgroundColorNoPaging" CssClass="Grid_border"
                                AutoGenerateColumns="False" OnRowDataBound="OnRowDataBoundECRLevel"
                                Width="500px" runat="server" >
                                <Columns>
                                 <asp:TemplateField>
                                    <ItemTemplate>
                                    <img alt = "" style="cursor: pointer"  src="../../Images/plus.png" />
                                    <asp:Panel ID="pnlHierarchyECR" runat="server" Style="display: none">
                                          <asp:GridView ID="gvECRLevelHierarchy" SkinID="GridView-NoBackgroundColorNoPaging"
                                            AutoGenerateColumns="False" CssClass="Grid_border"
                                            Width="100px" runat="server" >
                                            
                                            </asp:GridView>
                                    </asp:Panel>
                                    </ItemTemplate>
                                </asp:TemplateField>
    
                                </Columns>
    
                                <EmptyDataTemplate>
                                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                                        <tr>
                                            <td align="center" valign="middle" class="EmptyTemplate_nav">
                                                <asp:Label ID="lblEmptyData" SkinID="Label-Bold-12PX" runat="server" Text="No record(s) found."></asp:Label>
                                            </td>
                                        </tr>
                                    </table>
                                </EmptyDataTemplate>                                                                 
                            </asp:GridView>
                            
                          </ContentTemplate>
                          <Triggers>
                          <asp:AsyncPostBackTrigger ControlID="ImgbtnSearch" EventName="Click" />
                          </Triggers>
                          </asp:UpdatePanel>
    
                            </div>
                            <%--END ECR Level--%>
    
    
    //---------------------------------------------------------------------------------------------------------
    //Code Behind
    //---------------------------------------------------------------------------------------------------------
    
    protected void OnRowDataBoundECRLevel(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[0].Text != string.Empty)
                {
                    DataTable dtlHierarchy = p_dsDashboardData.Tables["ECRLevel"].Copy();
                    DataView dvHierarchy = dtlHierarchy.DefaultView;
                    dvHierarchy.RowFilter = "Vertical = '" + e.Row.Cells[0].Text + "'";
    
                    DataTable dtlFinalResult = dvHierarchy.ToTable();
                    dtlFinalResult.Columns.Remove("Vertical");
    
                    GridView gvECRHierarchy = e.Row.FindControl("gvECRLevelHierarchy") as GridView;
                    SetupGrid(dtlFinalResult, gvECRHierarchy, true, true, true);
    
                    
                }
            }

    Friday, July 8, 2016 10:12 AM

Answers

  • User-1034726716 posted

    Your child GridView is inside a Panel control. So try accessing the panel first before your child gridview:

    Panel p = (Panel)e.Row.FindControl("pnlHierarchyECR");
    if(p != null){
    	GridView gvECRHierarchy = p.FindControl("gvECRLevelHierarchy") as GridView;
    }

    PS: You may need to bind your child GridView within RowCreated event of your parent GridView.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 8, 2016 3:40 PM
  • User61956409 posted

    Hi minu88,

    Firstly, please set breakpoint and debug code of OnRowDataBoundECRLevel event to make sure if the row could meet condition

    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[0].Text != string.Empty)

    Secondly, I create a sample to reproduce the issue, I find we could find the child GridView inside a panel from parent GridView row on parent GridView  OnRowDataBound event using FindControl() method. Please debug the code and check the controls collection of  GridView row.

    Sample code

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <%#Eval("deliveryId") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <%#Eval("orderId") %>
                    <asp:Panel ID="pnlHierarchyECR" runat="server">
                        <asp:GridView ID="gvECRLevelHierarchy" runat="server"></asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnok" runat="server" Text="View" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            GridView gdv = (GridView)e.Row.FindControl("gvECRLevelHierarchy");
                    
    
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Data");
    
            dt.Rows.Add("1", "data1");
            dt.Rows.Add("2", "data2");
            dt.Rows.Add("3", "data3");
    
            gdv.DataSource = dt;
            gdv.DataBind();
        }
    }
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2016 10:01 AM

All replies

  • User-1034726716 posted

    Your child GridView is inside a Panel control. So try accessing the panel first before your child gridview:

    Panel p = (Panel)e.Row.FindControl("pnlHierarchyECR");
    if(p != null){
    	GridView gvECRHierarchy = p.FindControl("gvECRLevelHierarchy") as GridView;
    }

    PS: You may need to bind your child GridView within RowCreated event of your parent GridView.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 8, 2016 3:40 PM
  • User61956409 posted

    Hi minu88,

    Firstly, please set breakpoint and debug code of OnRowDataBoundECRLevel event to make sure if the row could meet condition

    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[0].Text != string.Empty)

    Secondly, I create a sample to reproduce the issue, I find we could find the child GridView inside a panel from parent GridView row on parent GridView  OnRowDataBound event using FindControl() method. Please debug the code and check the controls collection of  GridView row.

    Sample code

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <%#Eval("deliveryId") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <%#Eval("orderId") %>
                    <asp:Panel ID="pnlHierarchyECR" runat="server">
                        <asp:GridView ID="gvECRLevelHierarchy" runat="server"></asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnok" runat="server" Text="View" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            GridView gdv = (GridView)e.Row.FindControl("gvECRLevelHierarchy");
                    
    
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Data");
    
            dt.Rows.Add("1", "data1");
            dt.Rows.Add("2", "data2");
            dt.Rows.Add("3", "data3");
    
            gdv.DataSource = dt;
            gdv.DataBind();
        }
    }
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2016 10:01 AM