On your Web form, drag a TreeView control over and name it TreeView1. Below is the control code. Remember to include an import for System.Data, System.Data.SqlClient, and Microsoft.Web.UI.WebControls:
Dim strConn As String = "server=.;database=Northwind;integrated security=true;"
Dim objConn As New SqlConnection(strConn)
Dim objDS As New DataSet
Dim daSuppliers As New SqlDataAdapter("SELECT CompanyName,SupplierID FROM Suppliers", objConn)
Dim daProducts As New SqlDataAdapter("SELECT ProductName, ProductID, SupplierID FROM Products", objConn)
daSuppliers.Fill(objDS, "dtSuppliers")
daProducts.Fill(objDS, "dtProducts")
objConn.Close()
objDS.Relations.Add("SuppToProd", _
objDS.Tables("dtSuppliers").Columns("SupplierID"), _
objDS.Tables("dtProducts").Columns("SupplierID"))
Dim nodeSupp, nodeProd As TreeNode
Dim rowSupp, rowProd As DataRow
For Each rowSupp In objDS.Tables("dtSuppliers").Rows
nodeSupp = New TreeNode
nodeSupp.Text = rowSupp("CompanyName")
nodeSupp.ID = rowSupp("SupplierID")
TreeView1.Nodes.Add(nodeSupp)
For Each rowProd In rowSupp.GetChildRows("SuppToProd")
nodeProd = New TreeNode
nodeProd.Text = rowProd("ProductName")
nodeProd.ID = rowProd("ProductID")
nodeSupp.Nodes.Add(nodeProd)
Next
Next
'clean up
objDS.Dispose()
daSuppliers.Dispose()
daProducts.Dispose()
objConn.Close()
objConn.Dispose()
|