3

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

1
  • On RowDataBound for the parent GridView set the ID for the child grid to the row key.
    – k0stya
    Jun 13, 2012 at 5:54

5 Answers 5

8

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;
        }
    }
0
4

I don't think you will be able to track it normally, but I would embed ID field into the hidden field and put this hidden field under TemplateField,

<ItemTemplate> 
    <asp:HiddenField ID="idOfYourHiddenField" 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> 

this way you can get its value by going

gvChild.Parent.FindControl("idOfYourHiddenField");
0
2

You Can Access The Parent of Child Gridview with the Parent Property. You must be Try This:

 GridView gvChild = (GridView)e.Row.FindControl("gvChild");
     Response.Write(gvChild.Parent);
2

You have to go 4 steps back and get the parent row like this

protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridViewRow gvMasterRow = (GridViewRow)e.Row.Parent.Parent.Parent.Parent;
            }
        }
0
<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:TemplateField>
                        <ItemTemplate>
                            <%# (((IDataItemContainer)Container.Parent.Parent.Parent).DataItem as MyClass).MyProperty %>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.