简体   繁体   中英

Gridview inside gridview inside gridview

I use this code to bind the gvOrders gridview nested in gvCustomers gridview. It works perfectly.

 Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim code As String = "1"
            AccessDataSource2.SelectCommand = "SELECT * FROM [Tabella4] WHERE ([d] = '" & code & "')"

            Dim gvOrders As GridView = TryCast(e.Row.FindControl("gvOrders"), GridView)
            gvOrders.DataSource = AccessDataSource2
            gvOrders.DataBind()
        End If

And this is the HTML side. There are three gridview: gvOrders2 inside gvOrders inside gvCustomers.

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
      DataKeyNames="a" OnRowDataBound="OnRowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <img alt = "" style="cursor: pointer" src="images/plus.png" />
                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                        <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                            <Columns>
             <asp:TemplateField>
                <ItemTemplate>
                    <img alt = "" style="cursor: pointer" src="images/plus.png" />
                    <asp:Panel ID="pnlOrders2" runat="server" Style="display: none">
                        <asp:GridView ID="gvOrders2" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                            <Columns>
                                <asp:BoundField ItemStyle-Width="150px" DataField="f" HeaderText="f" />
                                <asp:BoundField ItemStyle-Width="150px" DataField="g" HeaderText="g" />
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>

                                <asp:BoundField ItemStyle-Width="150px" DataField="d" HeaderText="d" />
                                <asp:BoundField ItemStyle-Width="150px" DataField="e" HeaderText="e" />
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField ItemStyle-Width="150px" DataField="a" HeaderText="a" >
<ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField ItemStyle-Width="150px" DataField="b" HeaderText="b" >
<ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>
        </Columns>
    </asp:GridView>

     <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
         DataFile="~/App_Data/db1.mdb" SelectCommand="SELECT * FROM [Tabella3]">
     </asp:AccessDataSource>
     <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
         DataFile="~/App_Data/db1.mdb" SelectCommand="SELECT * FROM [Tabella4]">
     </asp:AccessDataSource>
     <asp:AccessDataSource ID="AccessDataSource3" runat="server" 
         DataFile="~/App_Data/db1.mdb" SelectCommand="SELECT * FROM [Tabella5]">
     </asp:AccessDataSource>

gvOrders2 gridview is nested inside the gvOrders gridview: How to populate the gvOrders2 gridview from AccessDataSource3? How to find the gvOrders2 control?

In the aspx, add the OnRowDataBound... text:

<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false"
CssClass = "ChildGrid" OnRowDataBound="gvOrders2_OnRowDataBound">

In your cs add the function:

Protected Sub gvOrders2_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

and in that function, use the same approach you used in OnRowDataBound. Something like:

If e.Row.RowType = DataControlRowType.DataRow Then

    AccessDataSource3.SelectCommand = "SELECT ..."

    Dim gvOrders2 As GridView = TryCast(e.Row.FindControl("gvOrders2"), GridView)
    gvOrders2.DataSource = AccessDataSource3
    gvOrders2.DataBind()
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM