17/01/08

Funzione per riempire una treeview da un dataset

Dim DSNWind As DataSet

Dim CNnwind As New SqlClient.SqlConnection("DATA SOURCE=servername;USER ID=;PASSWORD=;INITIAL CATALOG=northwind;")

Dim DACustomers As New SqlClient.SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE country = 'Germany'", CNnwind)

Dim DAOrders As New SqlClient.SqlDataAdapter("SELECT CustomerID, OrderID, OrderDate, ShippedDate, ShipVia, Freight FROM orders where customerid in (select customerid from customers where country = 'Germany')", CNnwind)

Dim DAOrderDetails As New SqlClient.SqlDataAdapter("Select * from [Order Details] where OrderID in (SELECT OrderID FROM orders where customerid in (select customerid from customers where country = 'Germany'))", CNnwind)

DSNWind = New DataSet()
CNnwind.Open()
DACustomers.Fill(DSNWind, "dtCustomers")
DAOrders.Fill(DSNWind, "dtOrders")
DAOrderDetails.Fill(DSNWind, "dtOrderDetails")
CNnwind.Close()
DSNWind.Relations.Add("CustToOrd", DSNWind.Tables("dtCustomers").Columns("CustomerID"), DSNWind.Tables("dtOrders").Columns("CustomerID")) DSNWind.Relations.Add("OrdToDet", DSNWind.Tables("dtOrders").Columns("OrderID"), DSNWind.Tables("dtOrderdetails").Columns("OrderID")) ''''''''''''''''''''''' TreeView1.Nodes.Clear()

Dim i, n As Integer
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = DSNWind.Tables("dtCustomers")
For Each parentrow In ParentTable.Rows
Dim parentnode As TreeNode
parentnode = New TreeNode(parentrow.Item(0))
TreeView1.Nodes.Add(parentnode)

Dim childrow As DataRow
Dim childnode As TreeNode
childnode = New TreeNode()
For Each childrow In parentrow.GetChildRows("CustToOrd")
childnode = parentnode.Nodes.Add(childrow(0) & " " & childrow(1) & " " & childrow(2))
childnode.Tag = childrow("OrderID")
Dim childrow2 As DataRow
Dim childnode2 As TreeNode
childnode2 = New TreeNode()
For Each childrow2 In childrow.GetChildRows("OrdToDet")
childnode2 = childnode.Nodes.Add(childrow2(0))
Next childrow2
Next childrow
Next parentrow

Nessun commento:

Posta un commento

Cosa ne pensi?