C# – nested gridview get parent row

asp.netcgridviewnet

I am using Nested GridViews where each row in the gridview has child gridView.
I am using RowDataBound Event of Parent GridView, to Binding Child GridView.
My Problem is that, how to get Parent GridView's Key on Child gridViews RowDataBound Event.

Below is example code:

<asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="1" AllowPaging="true" PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" SkinID="GVCenter" onrowdatabound="gvParent_RowDataBound">
   <Columns>
        <asp:BoundField DataField="Name" />
        <asp:TemplateField>
           <ItemTemplate>
               <asp:GridView ID="gvChild"  DataKeyNames="ID" runat="server" AutoGenerateColumns="false" ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
                  <Columns>
                     <asp:BoundField DataField="Name" />                     
                  </Columns>
                </asp:GridView>
           </ItemTemplate>
        </asp:TemplateField>
   </Columns>
</asp:GridView>

Here is the code behind:

    protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gvChild= (GridView)e.Row.FindControl("gvChild");
            gvChild.DataSource = getChildObj();
            gvChild.DataBind();
        }
    }

   protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Here I need to get the parent gridview Row Key
        }
    }

Hope the above code explains all the scenario.

Thanks in advance
Sandy

Best Answer

Try this

 <asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="10" AllowPaging="true"
            PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" OnRowDataBound="gvParent_RowDataBound">
            <Columns>
                <asp:BoundField DataField="Name" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HiddenField ID="HdnID" runat="server" Value='<%# Eval("ID") %>' />
                        <asp:GridView ID="gvChild" DataKeyNames="ID" runat="server" AutoGenerateColumns="false"
                            ShowHeader="false" OnRowDataBound="gvChild_RowDataBound">
                            <Columns>
                                <asp:BoundField DataField="Name" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code behind

protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gvChild = (GridView)e.Row.FindControl("gvChild");
            gvChild.DataSource = GetData();
            gvChild.DataBind();
        }
    }

    protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ID = ((HiddenField)e.Row.Parent.Parent.Parent.FindControl("HdnID")).Value;
        }
    }
Related Topic