要从 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