In actual project development, I found a way to achieve the hierarchical effect using the gridview, and finally the following:
The following is the implementation code:
ASPX page CopyCode The Code is as follows: <asp: gridview id = "gridview1" skinid = "gridview" runat = "server" autogeneratecolumns = "false"
Width = "100%" allowpaging = "true" pagesize = "10">
<Columns>
<Asp: templatefield headerstyle-width = "1%">
<Itemtemplate>
<Asp: Image id = "imgflag" runat = "server" imageurl = "~ /Images/dn.gif "/>
</Itemtemplate>
<Itemstyle horizontalalign = "center"/>
</ASP: templatefield>
<Asp: boundfield datafield = "customerid" headertext = "customer No." headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "companyName" headertext = "company name" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "contactname" headertext = "Contact" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "contacttitle" headertext = "Contact title" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "Address" headertext = "contact address" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "city" headertext = "city" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: templatefield headertext = "contact number" headerstyle-width = "10%">
<Itemtemplate>
<Asp: hiddenfield id = "hcustomerid" runat = "server" value = '<% # eval ("customerid") %>'/>
<% # Eval ("phone") %>
<Tr runat = "server" id = "trcustomers" style = "display: none">
<TD>
</TD>
<TD colspan = "7">
<Asp: gridview id = "gridvieworders" skinid = "gridview" runat = "server" width = "100%" autogeneratecolumns = "false"
Onrowdatabound = "doonrowdataboundorders">
<Columns>
<Asp: templatefield headerstyle-width = "1%">
<Itemtemplate>
<Asp: Image id = "imgflag1" runat = "server" imageurl = "~ /Images/dn.gif "/>
</Itemtemplate>
<Itemstyle horizontalalign = "center"/>
</ASP: templatefield>
<Asp: boundfield datafield = "orderid" headertext = "Order No." headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "orderdate" headertext = "Order Date" headerstyle-width = "10%" dataformatstring = "{0: d}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "requireddate" headertext = "required date" headerstyle-width = "10%"
Dataformatstring = "{0: d}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "shippeddate" headertext = "date of shipment" headerstyle-width = "10%"
Dataformatstring = "{0: d}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "freight" headertext = "weight" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "shipname" headertext = "shipping name" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: templatefield headertext = "zip code" headerstyle-width = "10%">
<Itemtemplate>
<Asp: hiddenfield id = "horderid" runat = "server" value = '<% # eval ("orderid") %>'/>
<% # Eval ("shippostalcode") %>
<Tr runat = "server" id = "trorders" style = "display: none">
<TD>
</TD>
<TD colspan = "7">
<Asp: gridview id = "gridvieworderdetails" skinid = "gridview" runat = "server" width = "100%"
Autogeneratecolumns = "false">
<Columns>
<Asp: boundfield datafield = "productname" headertext = "Product Name" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "quantity" headertext = "quantity" headerstyle-width = "10%" dataformatstring = "{0: n}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "unitprice" headertext = "unit price" headerstyle-width = "10%" dataformatstring = "{0: n}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "oprice" headertext = "Total" headerstyle-width = "10%" dataformatstring = "{0: n}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "discount" headertext = "discount" headerstyle-width = "10%" dataformatstring = "{0: p}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "pprice" headertext = "" headerstyle-width = "10%" dataformatstring = "{0: n}">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "quantityperunit" headertext = "Packing type" headerstyle-width = "10%">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
</Columns>
</ASP: gridview>
</TD>
</Tr>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
</TD>
</Tr>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
Background code Copy code The Code is as follows: protected sub page_load (byval sender as object, byval e as system. eventargs) handles me. Load
If not ispostback then
Bindgridview ()
End if
End sub
'1. First find the MERs data
Public sub bindgridview ()
Dim strsql as string = "select * from MERs"
Dim dttable as datatable = getdatatable (strsql)
Scount = dttable. Rows. Count
Me. gridview1.datasource = dttable
Me. gridview1.databind ()
End sub
What to do when 'customer' data is bound
Protected sub gridview1_rowdatabound (byval sender as object, byval e as system. Web. UI. webcontrols. gridviewroweventargs) handles gridview1.rowdatabound
If E. Row. rowtype = datacontrolrowtype. datarow then
Dim hcustomerid as string = ctype (E. Row. findcontrol ("hcustomerid"), hiddenfield). Value
Dim trcustomers as htmltablerow = ctype (E. Row. findcontrol ("trcustomers"), htmltablerow)
If hcustomerid. Trim. length> 0 then
'Move the mouse to change the CSS style.
E. Row. Attributes. Add ("onmouseover", "currentcolor = This. classname; this. classname = 'mouseover ';")
E. Row. Attributes. Add ("onmouseout", "This. classname = currentcolor ;")
'Locate all orders of this customer
Dim strsql as string = "select * from orders where customerid = '" & hcustomerid &"'"
Dim dttable as datatable = getdatatable (strsql)
Dim imgflag as image = ctype (E. Row. findcontrol ("imgflag"), image)
If dttable. Rows. Count> 0 then
Dim gridvieworders as gridview = ctype (E. Row. findcontrol ("gridvieworders"), gridview)
Gridvieworders. datasource = dttable
Gridvieworders. databind ()
'Set the JavaScript For clicking a row
Dim strscript as new stringbuilder
Strscript. append ("Var OBJ = Document. getelementbyid ('" & trcustomers. clientid &"');")
Strscript. append ("Var objimg = Document. getelementbyid ('" & imgflag. clientid &"');")
Strscript. append ("If (obj. style. Display = 'None '){")
Strscript. append ("obj. style. Display = '';")
Strscript. append ("objimg. src = 'images/up.gif ';")
Strscript. append ("} else {")
Strscript. append ("obj. style. Display = 'none ';")
Strscript. append ("objimg. src = 'images/dn.gif ';")
Strscript. append ("}")
E. Row. Attributes. Add ("onclick", strscript. tostring)
Else
Imgflag. imageurl = "~ /Images/up.gif"
End if
End if
End if
End sub
Protected sub doonrowdataboundorders (byval sender as object, byval e as system. Web. UI. webcontrols. gridviewroweventargs)
If E. Row. rowtype = datacontrolrowtype. datarow then
Dim horderid as string = ctype (E. Row. findcontrol ("horderid"), hiddenfield). Value
Dim trorders as htmltablerow = ctype (E. Row. findcontrol ("trorders"), htmltablerow)
If horderid. Trim. length> 0 then
'Move the mouse to change the CSS style.
E. Row. Attributes. Add ("onmouseover", "currentcolor = This. classname; this. classname = 'mouseover ';")
E. Row. Attributes. Add ("onmouseout", "This. classname = currentcolor ;")
'Locate all order details of this Order
Dim strsql as string = "select products. productname, [Order Details]. unitprice, [Order Details]. quantity, [Order Details]. unitprice * [Order Details]. quantity as oprice, [Order Details]. discount ,"&_
"([Order Details]. unitprice * [Order Details]. Quantity) * (1.00-[Order Details]. Discount) as pprice, products. quantityperunit "&_
"From [Order Details] inner join products on [Order Details]. productid = products. productid "&_
"Where orderid = '" & horderid &"'"
Dim dttable as datatable = getdatatable (strsql)
Dim imgflag1 as image = ctype (E. Row. findcontrol ("imgflag1"), image)
If dttable. Rows. Count> 0 then
Dim gridvieworderdetails as gridview = ctype (E. Row. findcontrol ("gridvieworderdetails"), gridview)
Gridvieworderdetails. datasource = dttable
Gridvieworderdetails. databind ()
'Set the JavaScript For clicking a row
Dim strscript as new stringbuilder
Strscript. append ("Var OBJ = Document. getelementbyid ('" & trorders. clientid &"');")
Strscript. append ("Var objimg = Document. getelementbyid ('" & imgflag1.clientid &"');")
Strscript. append ("If (obj. style. Display = 'None '){")
Strscript. append ("obj. style. Display = '';")
Strscript. append ("objimg. src = 'images/up.gif ';")
Strscript. append ("} else {")
Strscript. append ("obj. style. Display = 'none ';")
Strscript. append ("objimg. src = 'images/dn.gif ';")
Strscript. append ("}")
E. Row. Attributes. Add ("onclick", strscript. tostring)
End if
End if
End if
End sub