1

我过去从未使用过 TreeViews,我想显示一个层次结构(n 级的递归关系)。数据(在数据集中可用 - 从数据库查询中检索)具有以下结构:

__ID__  | __NAME__  | __PARENT__ 
 1      | Patrick   |       
 2      | Mark      |
 3      | Scott     | 2
 4      | Jason     | 
 5      | Julian    |
 6      | John      | 6
 7      | Steve     |
 8      | George    | 1 
 9      | Robert    | 1
 10     | Rodney    | 8

我正在尝试产生以下输出

- Patrick [1]
  - George [8]
    - Rodney [10]
  - Robert [9]

- Mark [2]
  - Scott [3]

- Julian [5]
  - John [6]

- Jason [4]

- Steve [7]

我正在尝试生成 Treeview 控件,但没有使用 Treeview 的经验。任何有关如何实现这一目标的反馈或示例将不胜感激。

4

2 回答 2

2

要从 DataTable 填充 TreeView,请尝试以下代码

Dim DataTable1 As New DataTable

Private Sub FillTestTable()
    DataTable1.Columns.Add("ID", GetType(Integer))
    DataTable1.Columns.Add("NAME", GetType(String))
    DataTable1.Columns.Add("PARENT", GetType(Integer))
    DataTable1.Columns.Add("LEVEL", GetType(Integer))

    DataTable1.Rows.Add(1, "Patrick")
    DataTable1.Rows.Add(2, "Mark")
    DataTable1.Rows.Add(3, "Scott", 2)
    DataTable1.Rows.Add(4, "Jason")
    DataTable1.Rows.Add(5, "Julian")
    DataTable1.Rows.Add(6, "John", 5)
    DataTable1.Rows.Add(7, "Steve")
    DataTable1.Rows.Add(8, "George", 1)
    DataTable1.Rows.Add(9, "Robert", 1)
    DataTable1.Rows.Add(10, "Rodney", 8)

    Dim i As Integer

    For i = 0 To DataTable1.Rows.Count - 1
        Dim ID1 As String = DataTable1.Rows(i).Item("ID").ToString
        DataTable1.Rows(i).Item("LEVEL") = FindLevel(ID1, 0)
    Next
End Sub

Private Function FindLevel(ByVal ID As String, ByRef Level As Integer) As Integer
    Dim i As Integer

    For i = 0 To DataTable1.Rows.Count - 1
        Dim ID1 As String = DataTable1.Rows(i).Item("ID").ToString
        Dim Parent1 As String = DataTable1.Rows(i).Item("PARENT").ToString

        If ID = ID1 Then
            If Parent1 = "" Then
                Return Level
            Else
                Level += 1
                FindLevel(Parent1, Level)
            End If
        End If
    Next

    Return Level
End Function

VB.NET WindowsForms 应用程序的代码

Private Sub CreateTree()
    Dim MaxLevel1 As Integer = CInt(DataTable1.Compute("MAX(LEVEL)", ""))

    Dim i, j As Integer

    For i = 0 To MaxLevel1
        Dim Rows1() As DataRow = DataTable1.Select("LEVEL = " & i)

        For j = 0 To Rows1.Count - 1
            Dim ID1 As String = Rows1(j).Item("ID").ToString
            Dim Name1 As String = Rows1(j).Item("NAME").ToString
            Dim Parent1 As String = Rows1(j).Item("PARENT").ToString

            If Parent1 = "" Then
                TreeView1.Nodes.Add(ID1, Name1)
            Else
                Dim TreeNodes1() As TreeNode = TreeView1.Nodes.Find(Parent1, True)

                If TreeNodes1.Length > 0 Then
                    TreeNodes1(0).Nodes.Add(ID1, Name1)
                End If
            End If
        Next
    Next
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    FillTestTable()
    CreateTree()
    TreeView1.ExpandAll()
End Sub

ASP.NET 应用程序的代码

Private Sub CreateTree()
    Dim MaxLevel1 As Integer = CInt(DataTable1.Compute("MAX(LEVEL)", ""))

    Dim i, j As Integer

    For i = 0 To MaxLevel1
        Dim Rows1() As DataRow = DataTable1.Select("LEVEL = " & i)

        For j = 0 To Rows1.Count - 1
            Dim ID1 As String = Rows1(j).Item("ID").ToString
            Dim Name1 As String = Rows1(j).Item("NAME").ToString
            Dim Parent1 As String = Rows1(j).Item("PARENT").ToString

            If Parent1 = "" Then
                TreeView1.Nodes.Add(New TreeNode(Name1, ID1))
            Else
                Dim Node1 As TreeNode = GetChildByValue(Parent1, TreeView1.Nodes)

                If Not Node1 Is Nothing Then
                    Node1.ChildNodes.Add(New TreeNode(Name1, ID1))
                End If
            End If
        Next
    Next
End Sub

Private Function GetChildByValue(ByVal ID1 As String, ByVal NodeCollection1 As TreeNodeCollection) As TreeNode
    For Each TreeNode1 As TreeNode In NodeCollection1
        If TreeNode1.Value = ID1 Then
            Return TreeNode1
        Else
            Dim TreeNode2 As TreeNode = GetChildByValue(ID1, TreeNode1.ChildNodes)

            If Not TreeNode2 Is Nothing Then
                Return TreeNode2
            End If
        End If
    Next

    Return Nothing
End Function

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    FillTestTable()
    CreateTree()
    TreeView1.ExpandAll()
End Sub
于 2013-07-20T22:36:58.613 回答
1

这里最重要的是有一个通用且适应性强的算法,能够执行所需的排序。一旦这到位,将值写入TreeView很简单,只需添加NodesChildNodes

    Dim NAME(10) As String
    Dim PARENT(10) As Integer
    Dim curID As Integer = 0

    curID = 1
    NAME(curID) = "Patrick [" & curID.ToString() & "]"
    PARENT(curID) = 0

    curID = curID + 1
    NAME(curID) = "Mark [" & curID.ToString() & "]"
    PARENT(curID) = 0

    curID = curID + 1
    NAME(curID) = "Scott [" & curID.ToString() & "]"
    PARENT(curID) = 2

    curID = curID + 1
    NAME(curID) = "Jason [" & curID.ToString() & "]"
    PARENT(curID) = 0

    curID = curID + 1
    NAME(curID) = "Julian [" & curID.ToString() & "]"
    PARENT(curID) = 0

    curID = curID + 1
    NAME(curID) = "John [" & curID.ToString() & "]"
    PARENT(curID) = 6

    curID = curID + 1
    NAME(curID) = "Steve [" & curID.ToString() & "]"
    PARENT(curID) = 0

    curID = curID + 1
    NAME(curID) = "George [" & curID.ToString() & "]"
    PARENT(curID) = 1

    curID = curID + 1
    NAME(curID) = "Robert [" & curID.ToString() & "]"
    PARENT(curID) = 1

    curID = curID + 1
    NAME(curID) = "Rodney [" & curID.ToString() & "]"
    PARENT(curID) = 8

    Dim completed As Boolean = False
    Dim firstIteration As Boolean = True
    Dim totIDs As Integer = 10

    Do
        curID = 0
        Do
            curID = curID + 1
            If (firstIteration) Then
                If (PARENT(curID) = 0 And TreeView1.FindNode(NAME(curID)) Is Nothing) Then
                    TreeView1.Nodes.Add(New TreeNode(NAME(curID)))
                End If
            Else
                If (PARENT(curID) > 0) Then
                    Dim targetNodes As TreeNodeCollection = TreeView1.Nodes
                    Dim count As Integer = 0

                    If (TreeView1.FindNode(NAME(curID)) Is Nothing) Then
                        For Each node As TreeNode In targetNodes
                            count = count + 1
                            If (node.Text.Contains("[" & PARENT(curID).ToString() & "]")) Then
                                node.ChildNodes.Add(New TreeNode(NAME(curID)))
                                Exit For
                            End If
                        Next
                    End If
                End If
            End If

        Loop While (curID < totIDs)

        If (firstIteration) Then
            firstIteration = False
        Else
            Exit Do 'Just two iterations
        End If
    Loop While (Not completed)

此代码依赖于两个数组(NAME(strings),其中还包括 [original position] 和PARENT(integers)),并执行包含直到“第二级”,即主节点和第一个子节点。

我想你将有足够的信息来理解如何处理TreeView和构建一个能够执行你想要的排序的算法。

于 2013-07-19T15:52:26.373 回答