nested gridview get parent row

后端 未结 5 1829
执笔经年
执笔经年 2021-01-20 22:04

I am using Nested GridViews where each row in the gridview has child gridView. I am using RowDataBound Event of Parent GridView, to Bindin

相关标签:
5条回答
  • 2021-01-20 22:24
    <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>
    

    0 讨论(0)
  • 2021-01-20 22:39

    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 讨论(0)
  • 2021-01-20 22:39

    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);
    
    0 讨论(0)
  • 2021-01-20 22:42

    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 讨论(0)
  • 2021-01-20 22:43

    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 讨论(0)
提交回复
热议问题