CodeMonkey
CodeMonkey

Reputation: 1156

Adding Child Nodes to Childe Nodes on TreeView in VB

I have a DataTable that can have 1 to 14 columns, i think. I wish to create a TreeView so that the data presented can be better sorted through. The problem is that when i loop through the datatable, it does not correctly format the tree. In fact, it only gets the root node and a child node, but it adds the child node multiple times. Now i could brute force it to work, but that is inefficient. So if anyone can help me out.

A data table that is used What it should look like in the tree

Code: (Ignore the .Count - 5)

Public Sub loadTreeView()

    compClass.treeView.Nodes.Clear()

    Dim row As DataRow
    Dim parentNode As TreeNode
    Dim childNode As TreeNode

    Dim count As Integer = compClass.dataTable.Columns.Count - 5

    Dim test(count) As TreeNode

    For Each row In compClass.dataTable.Rows

        For i As Integer = 0 To count

            If i = 0 Then
                test(i) = searchNode(row.Item(i).ToString())
                If test(i) IsNot Nothing Then
                Else
                    test(i) = New TreeNode(row.Item(i).ToString())
                    compClass.treeView.Nodes.Add(test(i))
                End If
                'parentNode = searchNode(row.Item(0).ToString())
                'If parentNode IsNot Nothing Then
                'Else
                '    parentNode = New TreeNode(row.Item(0).ToString())
                '    compClass.treeView.Nodes.Add(parentNode)
                'End If
            Else
                childNode = searchNode(row.Item(i - 1).ToString())
                If childNode IsNot Nothing Then
                    test(i) = New TreeNode(row.Item(i).ToString())
                    test(i - 1).Nodes.Add(test(i))
                End If
                'parentNode = searchNode(row.Item(i - 1).ToString())
                'If parentNode IsNot Nothing Then
                '    childNode = New TreeNode(row.Item(i).ToString())
                '    parentNode.Nodes.Add(childNode)
                'End If
            End If
        Next
    Next
End Sub

Private Function searchNode(ByVal nodeText As String)
    For Each node As TreeNode In compClass.treeView.Nodes
        If node.Text = nodeText Then
            Return node
        End If
    Next

End Function

Upvotes: 0

Views: 3280

Answers (1)

Duha
Duha

Reputation: 829

I want to tell you if you want to add child in the tree you want the parent first for example to add "9" to "2014" you set "2014" as parent node then you add "9"

and if you want to search node in tree view use Nodes.Find(key), your method for search nodes search the root nodes only.

finally use key when add node , its the way for know the parent node and current node if it duplicate or not.

for example: 2014 ->9 ->AL
"9" there key is "20149" when add AL element check if "20149" is exist if yes it is a parent node then Check key "20149AL" if not exist then add node "AL" ......etc

try this code,its work with me :)

  Public Sub loadTreeView()
    compClass.treeView.Nodes.Clear()
    Dim parentNode As TreeNode
    Dim childNode As TreeNode
    Dim count As Integer = compClass.dataTable.Columns.Count - 1
    Dim row As DataRow
    Dim ItemKey As String

    For Each row In compClass.dataTable.Rows
        Dim test(count) As TreeNode
        ItemKey = Nothing
        For i As Integer = 0 To count
            If i = 0 Then
                test(i) = searchNode(row.Item(i).ToString())
                If test(i) IsNot Nothing Then
                Else
                    test(i) = New TreeNode(row.Item(i).ToString())
                    compClass.treeView.Nodes.Add(row.Item(i).ToString(), row.Item(i).ToString()) 'Add item and key for item
                End If
                ItemKey = row.Item(i).ToString()
            Else
                parentNode = searchNode(ItemKey)
                childNode = searchNode(ItemKey & row.Item(i).ToString())

                If childNode IsNot Nothing Then
                    ItemKey &= row.Item(i).ToString()
                ElseIf parentNode IsNot Nothing Then
                    test(i) = New TreeNode(row.Item(i).ToString())
                    parentNode.Nodes.Add(ItemKey & row.Item(i).ToString(), row.Item(i).ToString())
                    ItemKey &= row.Item(i).ToString()
                End If
            End If
        Next
    Next
End Sub

Private Function searchNode(ByVal nodeKey As String) As TreeNode
    Dim FoundNodes As TreeNode() = compClass.treeView.Nodes.Find(nodeKey, True)
    If FoundNodes IsNot Nothing AndAlso FoundNodes.Length > 0 Then
        Return FoundNodes(0)
    End If
    Return Nothing
End Function

Upvotes: 2

Related Questions