Wednesday, April 11, 2007

Populate Treeview in .net

Dim DBConn As New SqlConnection("Server=localhost;Persist Security Info=False;User ID=###;Password=;Initial Catalog=####;Data Source=#####")
DBConn.Open()

'Create DataAdaptor for Customers and Orders
'Table and fetch record from the Database
Dim DBCustAdap As New SqlDataAdapter("Select * From TA_PART1", DBConn)
Dim DBOrdAdap As New SqlDataAdapter("Select * From TA_PART2", DBConn)

'Create DataSet object and fill DataAdapter
Dim DSCustOrder As DataSet = New DataSet
DBCustAdap.Fill(DSCustOrder, "TA_PART1")
DBOrdAdap.Fill(DSCustOrder, "TA_PART2")

'Add Realation using Relation Attribute
DSCustOrder.Relations.Add("CustomerOrders", DSCustOrder.Tables("TA_PART1").Columns("f_part1_ta_part1"), DSCustOrder.Tables("TA_PART2").Columns("f_part1_ta_part2"))

'Fill the TreeView control Nodes using For Loop
For Each PRow As DataRow In DSCustOrder.Tables("TA_PART1").Rows

Dim PNode As New TreeNode(PRow("f_part1_ta_part1"))
Me.tvDisplayRecord.Nodes.Add(PNode)

For Each CRow As DataRow In PRow.GetChildRows("CustomerOrders")
PNode.ChildNodes.Add(New TreeNode(CRow("f_part2_ta_part2")))
Next
PNode.CollapseAll()
Next

'Close Connection and Dispose all database objects
DBCustAdap.Dispose()
DBCustAdap = Nothing
DBOrdAdap.Dispose()
DBOrdAdap = Nothing
DBConn.Close()
DBConn = Nothing

No comments:

Locations of visitors to this page
DROP ME A MSG :D