Vb.net 在VB中向TreeView上的Child节点添加子节点

Vb.net 在VB中向TreeView上的Child节点添加子节点,vb.net,treeview,Vb.net,Treeview,我想,我有一个数据表,可以有1到14列。我希望创建一个树状视图,以便对显示的数据进行更好的排序。问题是,当我循环遍历datatable时,它没有正确格式化树。实际上,它只获取根节点和子节点,但它多次添加子节点。现在我可以强迫它工作,但这是低效的。如果有人能帮我的话 代码:(忽略.Count-5) 我想告诉您,如果要在树中添加子对象,您首先要添加父对象 例如,将“9”添加到“2014” 将“2014”设置为父节点,然后添加“9” 如果要在树状视图中搜索节点,请使用Nodes.Find(键),搜

我想,我有一个数据表,可以有1到14列。我希望创建一个树状视图,以便对显示的数据进行更好的排序。问题是,当我循环遍历datatable时,它没有正确格式化树。实际上,它只获取根节点和子节点,但它多次添加子节点。现在我可以强迫它工作,但这是低效的。如果有人能帮我的话

代码:(忽略.Count-5


我想告诉您,如果要在树中添加子对象,您首先要添加父对象 例如,将“9”添加到“2014” 将“2014”设置为父节点,然后添加“9”

如果要在树状视图中搜索节点,请使用Nodes.Find(键),搜索节点的方法仅搜索根节点

最后在添加节点时使用键,这是知道父节点和当前节点是否重复的方法

例如: 2014->9->AL
“9”这里的键是“20149” 添加AL元素时,检查“20149”是否存在如果是,则检查键“20149AL”如果不存在,则添加节点“AL”

试试这段代码,它对我有用:)


假设它看起来像表格布局下的图像。如果可以,我会尝试一下。它看起来很结实。我很快会让你知道的。
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
  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