可以尝试一下这个,对于顺序没有任何要求,可惜Nodes没有contains方法,否则应该会写得更精简:
Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim tnd As WinForm.TreeNode
trv.Nodes.Clear()
For Each dr As DataRow In DataTables("表A").DataRows
Dim lst As New List(of String)
lst.Add(dr("第二列"))
Do
lst.Insert(0,dr("第一列"))
dr = DataTables("表A").Find("第二列 = '" & dr("第一列") & "'")
If dr is Nothing Then
Exit Do
End If
Loop
Dim Nodes As Object = trv.Nodes
For Each val As String In lst
Dim nm As String = ""
For Each nd As WinForm.TreeNode In Nodes
If nd.Name = Val Then
nm = val
Exit For
End If
Next
If nm = ""
nodes.Add(val,val)
End If
Nodes = nodes(val).nodes
Next
Next
下次给Nodes 加个Contains方法,代码可以简化为:
Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim tnd As WinForm.TreeNode
trv.Nodes.Clear()
For Each dr As DataRow In DataTables("表A").DataRows
Dim lst As New List(of String)
lst.Add(dr("第二列"))
Do
lst.Insert(0,dr("第一列"))
dr = DataTables("表A").Find("第二列 = '" & dr("第一列") & "'")
If dr is Nothing Then
Exit Do
End If
Loop
Dim Nodes As Object = trv.Nodes
For Each val As String In lst
If Nodes.Contains(val) = False Then
nodes.Add(val,val)
End If
Nodes = nodes(val).nodes
Next
Next
[此贴子已经被作者于2010-12-7 18:10:22编辑过]