Results 1 to 3 of 3

Thread: [2003] How to populate a treeview?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    31

    [2003] How to populate a treeview?

    I have a table with the structure COMPANY(company_ID, company_name, parent_company_ID). In case, parent_company_ID is the Parent ID of the company (all parent and sub-company are stored in COMPANY table). I want to show the structure of company on the treeview (may be a lot of node level). I have ran these codes:
    Code:
    Dim rd As SqlClient.SqlDataReader = get_recordset("SELECT * FROM company ORDER BY company_ID")
            While rd.Read
                If rd("parent_company_ID") = 0 Then 'root level
                    Dim nd As System.Windows.Forms.TreeNode
                    nd = tv.Nodes.Add(rd("company_name"))
                    nd.Tag = rd("company_ID")
                Else
                    For Each nod As TreeNode In tv.Nodes
                        If nod.Tag.ToString = rd("parent_company_ID").ToString Then
                            Dim nd As System.Windows.Forms.TreeNode = nod.Nodes.Add(rd("company_name"))                        
                            nd.Tag = rd("company_ID")
                            Exit For
                        End If
                    Next nod
                End If
            End While
    But I 've got a treeview with two level (with company belongs to more than 3 level, they can not appearance on the treeview)

    Does anyone help me to populate the treeview on this case?

    Regards,
    Hai

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2003] How to populate a treeview?

    All trees are recursive structures. Whenever you want to populate a TreeView you should think recursion as a first option:
    vb.net Code:
    1. Private Sub LoadChildNodes(ByVal parentID As Integer, _
    2.                            ByVal nodes As TreeNodeCollection, _
    3.                            ByVal table As DataTable)
    4.     Dim filterExpression As String
    5.  
    6.     If parentID = -1 Then
    7.         'Get all the rows with no parent.
    8.         filterExpression = "ParentID IS Null"
    9.     Else
    10.         'Get all child rows of the specified parent.
    11.         filterExpression = "ParentID = " & parentID
    12.     End If
    13.  
    14.     Dim childNode As TreeNode
    15.  
    16.     For Each childRow As DataRow In table.Select(filterExpression)
    17.         'Add the new node.
    18.         childNode = nodes.Add(CStr(childRow("Name")))
    19.  
    20.         'Load all children of the new node.
    21.         Me.LoadChildNodes(CInt(childRow("ID")), _
    22.                           childNode.Nodes, _
    23.                           table)
    24.     Next childRow
    25. End Sub
    You would start the ball rolling like so:
    vb.net Code:
    1. Me.LoadChildNodes(-1, Me.TreeView1.Nodes, myDataTable)
    Obviously you will have to change the variable and column names to match those in your own app.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    31

    Re: [2003] How to populate a treeview?

    Thank you jmcilhinney,

    It's done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width