I am using Nested GridViews where each row in the gridview has child gridView. I am using RowDataBound Event of Parent GridView, to Bindin
<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>
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");
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);
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;
}
}
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;
}
}