monica73174
asked on
Adding nodes to a treeview control
How do I programatically add nodes to a treeview control
I found some code from microsoft but its for .net 1.1.
I found some code from microsoft but its for .net 1.1.
Private Sub bindRegions()
Dim connectionString As String = ConfigurationManager.ConnectionStrings("PCI").ConnectionString
Dim conn As SqlConnection
conn = New SqlConnection(connectionString)
Dim DSRegions As DataSet
Dim DARegions As New SqlClient.SqlDataAdapter("SELECT RegionID, RegionName FROM Region", conn)
Dim DACountries As New SqlClient.SqlDataAdapter("SELECT CountryID, CountryName FROM Country", conn)
Dim DASubRegions As New SqlClient.SqlDataAdapter("Select SubRegionID, SubRegionID from SubRegion", conn)
DSRegions = New DataSet()
conn.Open()
DARegions.Fill(DSRegions, "dtRegions")
DACountries.Fill(DSRegions, "dtCountries")
DASubRegions.Fill(DSRegions, "dtSubRegions")
'Close the connection to the data store; free up the resources
conn.Close()
'Create a data relation object to facilitate the relationship between the Customers and Orders data tables.
DSRegions.Relations.Add("RegionToCountry", DSRegions.Tables("dtRegions").Columns("RegionID"), DSRegions.Tables("dtCountries").Columns("RegionID"))
DSRegions.Relations.Add("CountryToSubRegion", DSRegions.Tables("dtCountries").Columns("CountryID"), DSRegions.Tables("dtSubRegions").Columns("CountryID"))
'''''''''''''''''''''''
TreeRegions.Nodes.Clear()
Dim i, n As Integer
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = DSRegions.Tables("dtRegions")
For Each parentrow In ParentTable.Rows
Dim parentnode As TreeNode
parentnode = New TreeNode(parentrow.Item(0))
TreeRegions.Nodes.Add(parentnode)
''''populate child'''''
'''''''''''''''''''''''
Dim childrow As DataRow
Dim childnode As TreeNode
childnode = New TreeNode()
For Each childrow In parentrow.GetChildRows("RegionToCountry")
childnode = parentnode.ChildNodes.Add( = (childrow(0) & " " & childrow(1) & " " & childrow(2))
childnode.Value = childrow("OrderID")
''''populate child2''''
''''''''''''''''''''''''''
Dim childrow2 As DataRow
Dim childnode2 As TreeNode
childnode2 = New TreeNode()
For Each childrow2 In childrow.GetChildRows("OrdToDet")
childnode2 = childnode.ChildNodes.Add.Add(childrow2(0))
Next childrow2
''''''''''''''''''''''''
Next childrow
'''''''''''''''
Next parentrow
End Sub
ASKER
thank you for your help but unfortunately that wasnt what I was looking for. I actually figured it out.
This is what I did to make it work.....
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = DSRegions.Tables("dtRegion s")
TreeRegions.ImageSet = TreeViewImageSet.Simple
'TreeRegions.
For Each parentrow In ParentTable.Rows
Dim parentnode As TreeNode
parentnode = New TreeNode(parentrow.Item("R egionName" ), parentrow.Item("RegionID") )
parentnode.ShowCheckBox = True
parentnode.NavigateUrl = ""
TreeRegions.Nodes.Add(pare ntnode)
''''populate child'''''
'''''''''''''''''''''''
Dim childrow As DataRow
Dim childnode As TreeNode
childnode = New TreeNode()
For Each childrow In parentrow.GetChildRows("Re gionToCoun try")
childnode = New TreeNode(childrow.Item("Co untryName" ), childrow.Item("CountryID") )
parentnode.ChildNodes.Add( childnode)
childnode.NavigateUrl = ""
''''populate child2''''
''''''''''''''''''''''''''
Dim childrow2 As DataRow
Dim childnode2 As TreeNode
childnode2 = New TreeNode()
For Each childrow2 In childrow.GetChildRows("Cou ntryToSubR egion")
childnode2 = New TreeNode(childrow2.Item("S ubRegionNa me"), childrow2.Item("SubRegionI D"))
childnode.ChildNodes.Add(c hildnode2)
childnode2.ShowCheckBox = True
childnode2.NavigateUrl = ""
Next childrow2
''''''''''''''''''''''''
Next childrow
''''''''''''''''
Next parentrow
This is what I did to make it work.....
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = DSRegions.Tables("dtRegion
TreeRegions.ImageSet = TreeViewImageSet.Simple
'TreeRegions.
For Each parentrow In ParentTable.Rows
Dim parentnode As TreeNode
parentnode = New TreeNode(parentrow.Item("R
parentnode.ShowCheckBox = True
parentnode.NavigateUrl = ""
TreeRegions.Nodes.Add(pare
''''populate child'''''
'''''''''''''''''''''''
Dim childrow As DataRow
Dim childnode As TreeNode
childnode = New TreeNode()
For Each childrow In parentrow.GetChildRows("Re
childnode = New TreeNode(childrow.Item("Co
parentnode.ChildNodes.Add(
childnode.NavigateUrl = ""
''''populate child2''''
''''''''''''''''''''''''''
Dim childrow2 As DataRow
Dim childnode2 As TreeNode
childnode2 = New TreeNode()
For Each childrow2 In childrow.GetChildRows("Cou
childnode2 = New TreeNode(childrow2.Item("S
childnode.ChildNodes.Add(c
childnode2.ShowCheckBox = True
childnode2.NavigateUrl = ""
Next childrow2
''''''''''''''''''''''''
Next childrow
''''''''''''''''
Next parentrow
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://harriyott.com/2007/03/adding-dynamic-nodes-to-aspnet-site.aspx
http://www.eggheadcafe.com/articles/20011013.asp