测试数据如下:
第一列 第二列
a a1
a bbb1
b bbb1
b d1
bbb1 dddddddd
代码如下:
Forms("窗口1").open()
Dim trv As WinForm.TreeView = Forms("窗口1").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("表B").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
测试结果:
此主题相关图片如下:screenshot2.jpg
![dvubb 图片点击可在新窗口打开查看](UploadFile/2010-12/2010129950020756.jpg)
说明:b-bbb1未产生下级
再来测试这段代码:
Forms("窗口1").open()
Dim trv As WinForm.TreeView = Forms("窗口1").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
结果和上面也是一样,未能生成b-bbb1的下级项
同样测试下面这段代码,结果也是同上:
Forms("窗口1").open()
Dim trv As WinForm.TreeView = Forms("窗口1").Controls("TreeView1")
Dim lst1 As List(of String) = DataTables("表A").GetUniqueValues("","第一列")
Dim lst2 As List(of String) = DataTables("表A").GetUniqueValues("","第二列")
Dim dic As New Dictionary(Of String, WinForm.TreeNode)
Dim tnd As WinForm.TreeNode
trv.Nodes.Clear()
For Each Val As String In Lst1
If lst2.Contains(val) = False Then
tnd = trv.Nodes.Add(val,val)
dic.Add(val,tnd)
End If
Next
For Each dr As DataRow In DataTables("表A").Select("", "_SortKey")
If dic.ContainsKey(dr("第一列")) Then
tnd = dic(dr("第一列")).Nodes.Add(dr("第二列"), dr("第二列"))
If dic.ContainsKey(dr("第二列")) Then
dic.Remove(dr("第二列"))
End If
dic.Add(dr("第二列"),tnd)
End If
Next
[此贴子已经被作者于2010-12-9 10:00:14编辑过]