Link to home
Start Free TrialLog in
Avatar of monica73174
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.  
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

Open in new window

Avatar of Shanmuga Sundaram D
Shanmuga Sundaram D
Flag of India image

Though I am not much experienced in ASP.Net, please check whether this helps. Ignore if this is not you need

http://harriyott.com/2007/03/adding-dynamic-nodes-to-aspnet-site.aspx
http://www.eggheadcafe.com/articles/20011013.asp
Avatar of monica73174
monica73174

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("dtRegions")
        TreeRegions.ImageSet = TreeViewImageSet.Simple
        'TreeRegions.
        For Each parentrow In ParentTable.Rows
            Dim parentnode As TreeNode
            parentnode = New TreeNode(parentrow.Item("RegionName"), parentrow.Item("RegionID"))
            parentnode.ShowCheckBox = True
            parentnode.NavigateUrl = ""
            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 = New TreeNode(childrow.Item("CountryName"), 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("CountryToSubRegion")
                    childnode2 = New TreeNode(childrow2.Item("SubRegionName"), childrow2.Item("SubRegionID"))
                    childnode.ChildNodes.Add(childnode2)
                    childnode2.ShowCheckBox = True
                    childnode2.NavigateUrl = ""
                Next childrow2
                ''''''''''''''''''''''''

            Next childrow
            ''''''''''''''''
        Next parentrow
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial