Search code examples
vb.nettreeview

How can someone select datat from a database table to a treeview control VB.NET?


VB.NET I have the following table containg data as shown in the picture below:

enter image description here

Now, I want to bring these data into the treeview control as it's shown in the other picture :

enter image description here

please , How can I do that in VB.NET ? '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' here is the code I ended up with:

Sub LoadTree()
    Dim sql As String = "SELECT Employees.EmpNum , Employees.EmpName , departments.depNum,  departments.depName,  Company.ID, Company.CompName FROM Company INNER JOIN departments ON Company.ID = departments.compNum INNER JOIN  Employees ON departments.depNum = Employees.depNum order by company.compName , departments.depName "
    Dim dt As New DataTable()
    Dim da As New SqlDataAdapter(sql, FrmMain.con)
    da.Fill(dt)

    TreeView1.Nodes.Clear()
    Dim CompanyName As String
    Dim depName As String
    Dim EmpName As String
    Dim tmpCName As String = ""
    Dim tmpDName As String = ""
    Dim sNode As New TreeNode
    Dim ssNode As New TreeNode

    'TreeView1.Nodes.Add("Dhafer")

    For Each dr As DataRow In dt.Rows
        CompanyName = dr("CompName").ToString()
        depName = dr("depName").ToString()
        EmpName = dr("EmpName").ToString()

        If tmpCName = CompanyName Then
            If tmpDName <> depName Then
                TreeView1.SelectedNode.Nodes.Add(depName).Nodes.Add(EmpName)
            End If               
        Else
            tmpCName = CompanyName
            tmpDName = depName
            sNode = TreeView1.Nodes.Add(CompanyName)
            TreeView1.SelectedNode = sNode

            TreeView1.SelectedNode.Nodes.Add(depName).Nodes.Add(EmpName)
        End If

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Solution

  • TreeNodes have a Name property, and a TreeNodeCollection (what a treeview.Nodes, or a treenode.Nodes proprety is) can find nodes based on the Name so your logic needs to be more like:

    For Each dr As DataRow In dt.Rows
    
        Dim coName = dr("CompName").ToString()
        Dim coNodeId = "co" & dr("ID").ToString()
    
        'find or create the company node
        Dim nodes = treeview.Nodes.Find(coNodeId, true)
        Dim coNode as TreeNode
    
        If nodes.Length = 0 Then 'didn't find: create and add
           coNode = New TreeNode() { Name = coNodeId, Text = coName }
           treeview.Nodes.Add(coNode)
        Else 'did find
           coNode = nodes(0)
        End If
    
    
        Dim depName = dr("depName").ToString()
        Dim depNodeId = "dep" & dr("depNum").ToString()
    
        'find or create the dep node under the co node
        nodes = coNode.Nodes.Find(depNodeId, true)
        Dim depNode as TreeNode
    
        If nodes.Length = 0 Then
           depNode = New TreeNode() { Name = depNodeId, Text = depName }
           coNode.Nodes.Add(depNode)
        Else
           depNode = nodes(0)
        End If
    
    
        'create the emp node
        Dim empName = dr("EmpName").ToString()
        ...
    

    For every row in the data table we:

    • try to find the co node, create it is it doesnt exist, then keep refere3nce to it
    • try to find the dep node under the co node, create if no exist, keep reference to it
    • add the emp node under the dep node

    Steps 1 ensures that the co node is found or created so step 2 can work. Step 2 ensures the dep node is created or found so that step 3 can work