我目前正在设计一种以树格式显示帐户的软件
我使用VS2012编码是在VB.NET中完成的
我正在使用TreeView控件
我将我的软件连接到SQL数据库,帐户表格为 如下:
ID - 名称 - ParentID
1 - Acc01 - 0
2 - Acc02 - 1
3 - Acc03 - 2
这个清单继续......
我知道如何创建数据集,我知道如何使用TreeView中的节点字符
但是如何在列表中进行编码并在TreeView控件中正确查看呢?
请记住,我的树可以分为3级,4级甚至更多 我需要代码是动态的
我尝试搜索,发现所有主题最多可以覆盖3个级别并自行输入每个级别(这意味着我必须为6级树做6次,依此类推......)
我之前使用过treeview,但我的最后一个案例不同,我有3个相关的表,并使用3批代码显示如下:
Private Sub CabinetTreeView()
sqlconnectionstring =
MainConfig.GetConnectionString(My.Settings.CurrentDB)
Dim DSCabinet As New DataSet
Dim CNCabinet As New SqlClient.SqlConnection(sqlconnectionstring)
Dim DADep As New SqlClient.SqlDataAdapter("select GUID,Code+'-'+Name as
Department,Notes from dep000", CNCabinet)
Dim DAFolder As New SqlClient.SqlDataAdapter("select
guid,'#'+convert(varchar(255),number)+': '+code+'-'+Name as Folder,DepGuid
from batch000", CNCabinet)
Dim DAFiles As New SqlClient.SqlDataAdapter("select
guid,batchguid,DocName from store000", CNCabinet)
DSCabinet = New DataSet()
CNCabinet.Open()
DADep.Fill(DSCabinet, "dtDep")
DAFolder.Fill(DSCabinet, "dtFolder")
DAFiles.Fill(DSCabinet, "dtFiles")
'Close the connection to the data store; free up the resources
CNCabinet.Close()
'Create a data relation object to facilitate the relationship between
the Customers and Orders data tables.
DSCabinet.Relations.Add("DeptoFold", DSCabinet.Tables("dtDep").Columns("GUID"), DSCabinet.Tables("dtFolder").Columns("DepGuid"))
DSCabinet.Relations.Add("FoldtoFile", DSCabinet.Tables("dtFolder").Columns("guid"), DSCabinet.Tables("dtFiles").Columns("Batchguid"))
'''''''''''''''''''''''
CabinetTree.Nodes.Clear()
Dim i, n As Integer
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = DSCabinet.Tables("dtDep")
For Each parentrow In ParentTable.Rows
Dim parentnode As TreeNode
parentnode = New TreeNode(parentrow.Item(1).ToString)
CabinetTree.Nodes.Add(parentnode)
parentnode.ImageIndex = 0
parentnode.SelectedImageIndex = 0
parentnode.Tag = parentrow(0).ToString
''''populate child'''''
'''''''''''''''''''''''
Dim childrow As DataRow
Dim childnode As TreeNode
childnode = New TreeNode()
For Each childrow In parentrow.GetChildRows("DeptoFold")
childnode = parentnode.Nodes.Add(childrow(1))
childnode.Tag = childrow(0).ToString
childnode.ImageIndex = 1
childnode.SelectedImageIndex = 2
''''populate child2''''
''''''''''''''''''''''''''
Dim childrow2 As DataRow
Dim childnode2 As TreeNode
childnode2 = New TreeNode()
For Each childrow2 In childrow.GetChildRows("FoldtoFile")
childnode2 = childnode.Nodes.Add(childrow2(2))
childnode2.Tag = childrow2(0).ToString
childnode2.ImageIndex = 3
childnode2.SelectedImageIndex = 3
Next childrow2
''''''''''''''''''''''''
Next childrow
'''''''''''''''
Next parentrow
End Sub
非常感谢提前
最诚挚的问候, Salah Mousa Basha