嵌套gridview获取父行

我正在使用嵌套GridViews,其中gridview中的每一行都有子gridView。 我正在使用Parent GridView的 RowDataBound事件来绑定Child GridView 。 我的问题是,如何获得Parent GridView的Key on Child gridViews RowDataBound事件。

以下是示例代码:

              

这是后面的代码:

  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 } } 

希望上面的代码解释了所有场景。

在此先感谢桑迪

试试这个

                 

代码背后

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

我不认为你能正常跟踪它,但我会将ID字段嵌入隐藏字段并将此隐藏字段放在TemplateField下,

         

这样你就可以通过去获得它的价值

 gvChild.Parent.FindControl("idOfYourHiddenField"); 

您可以使用属性访问子Gridview的父级 。 你一定要试试这个:

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

您必须返回4步并获得这样的父行

 protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GridViewRow gvMasterRow = (GridViewRow)e.Row.Parent.Parent.Parent.Parent; } } 
          <%# (((IDataItemContainer)Container.Parent.Parent.Parent).DataItem as MyClass).MyProperty %>