?
Solved

Adding nodes to a treeview control

Posted on 2008-02-07
4
Medium Priority
?
3,254 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:monica73174
3 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20842277
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
0
 

Author Comment

by:monica73174
ID: 20851546
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
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 21236472
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question