在VB中的TreeView上将子节点添加到Childe节点

时间:2014-09-12 21:46:39

标签: vb.net treeview

我认为我有一个可以有1到14列的DataTable。我希望创建一个TreeView,以便可以更好地对所显示的数据进行排序。问题是当我遍历数据表时,它没有正确格式化树。实际上,它只获取根节点和子节点,但它多次添加子节点。现在我可以强制它发挥作用,但这是低效的。所以,如果有人可以帮助我。

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

代码:(忽略.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

1 个答案:

答案 0 :(得分:2)

我想告诉您是否要在树中添加您想要父母的孩子 例如将“9”添加到“2014” 您将“2014”设置为父节点,然后添加“9”

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

最后在添加节点时使用密钥,它是知道父节点和当前节点是否重复的方式。

例如: 2014 - > 9 - > AL
“9”那里的关键是“20149” 当添加AL元素时检查“20149”是否存在如果是,则它是父节点然后检查键“20149AL”如果不存在则添加节点“AL”......等等

试试这段代码,它与我合作:)

  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