【问题标题】:nested gridview get parent row嵌套gridview获取父行
【发布时间】:2012-06-13 05:26:54
【问题描述】:

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

下面是示例代码:

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

下面是代码:

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

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

提前致谢 桑迪

【问题讨论】:

  • 在父 GridView 的 RowDataBound 上,将子网格的 ID 设置为行键。

标签: c# asp.net .net gridview


【解决方案1】:

试试这个

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

后面的代码

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

【讨论】:

    【解决方案2】:

    我认为你不能正常跟踪它,但我会在隐藏字段中嵌入 ID 字段并将这个隐藏字段放在 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> 
    

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

    gvChild.Parent.FindControl("idOfYourHiddenField");
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        你必须后退 4 步才能得到这样的父行

        protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        GridViewRow gvMasterRow = (GridViewRow)e.Row.Parent.Parent.Parent.Parent;
                    }
                }
        

        【讨论】:

          【解决方案5】:
          <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>
          

          【讨论】: