数据列表到树状视图 [英] Data list to treeview
问题描述
我想知道是否能为您提供帮助,请记住我是VB的新手,我正在努力通过VB.net将数据映射到树状视图中.
我在1个公司下有部门和团队等的以下数据结构,每个数据都有一个父级和子级ID,以映射到相关的职能部门.
请参阅以下内容:-
Hi,
I''m wondering if you can help me and please bear in mind that I''m relatively new to VB, I''m struggling to map data into a treeview with VB.net.
I have the following data structure of departments and teams etc under 1 company, each with a parent and child Id to map to the relavent function/department.
Please see the following :-
Parent Id Name Child Id Type
1000 Company1 Company
1001 Function1 1000 Function
1002 Function1 1000 Function
1003 Function1 1000 Function
1004 Department1 1001 Department
1005 Department2 1001 Department
1006 Department3 1002 Department
1007 Department4 1003 Department
1008 MainTeam1 1004 Team
1009 MainTeam2 1004 Team
1010 MainTeam3 1005 Team
1011 MainTeam4 1005 Team
1012 MainTeam5 1006 Team
1013 MainTeam6 1007 Team
1014 Subteam1 1008 SubTeam
1015 Subteam2 1008 SubTeam
1016 Subteam3 1009 SubTeam
1017 Subteam4 1009 SubTeam
1018 Subteam5 1010 SubTeam
1019 Subteam6 1011 SubTeam
1020 Subteam7 1012 SubTeam
1021 Subteam8 1013 SubTeam
1022 Sub-Subteam1 1015 SubTeam
1023 Sub-Subteam2 1016 SubTeam
1024 Sub-Subteam2 1017 SubTeam
我已将以上数据加载到一个结构数组OD中,该数组用于迭代查找我的父子关系,以便将特定的子节点添加到父节点.
鉴于我知道根公司的父ID,因此我在数组中查找父公司,并将其分配给树的根,如下所示:-
I have loaded the above data into a stucture array OD which I use to iterate through to find my parent and child relationship in order to add the specific child node to the parent node.
Given that I know the root company Parent Id I for-next through the array to find the company and assign the name to the root of my tree as follows:-
For i = 0 To UBound(OD)
If OD(i).Parent_Id = "1000" Then
PId = OD(i).Parent_Id
Exit For
End If
Next
Dim myNode As New TreeNode
myNode = TreeView1.Nodes.Add(OD(i).Name & " - " & OD(i).Sector)
然后,我将每个函数添加如下:-
I then add each function as follows :-
For i = 0 To UBound(OD) 'find each child under parent
If OD(i).Child_Id = PId Then
myNode.Nodes.Add(OD(i).Name)
End If
Next
我认为从这里开始,我需要跳过每个部门,然后像这样添加团队:-
From here I think I need to skip through each department and then add the teams as such :-
Index = 0
For Each n In funcNode.Nodes
PId = findParentID(n.Text)
For i = 0 To UBound(OD)
If PId = OD(i).Child_Id Then
funcNode.Nodes(Index).Nodes.Add(OD(i).Name)
End If
Next
Index = Index + 1
Next
Private Function findParentID(ByVal n As String) As String
Dim i As Long
findParentID = ""
For i = 0 To UBound(OD)
If n = OD(i).Name Then findParentID = OD(i).Parent_Id
Next
End Function
然后在每个函数下添加每个部门,然后运行类似的代码以添加MainTeam,如下所示:-
This then adds each department under each function, I then run a similar piece of code to add the MainTeams as follows :-
Dim DepNode As New TreeNode(0)
Dim n1 As TreeNode
For Each n In funcNode.Nodes
DepNode = TreeView1.Nodes(0).Nodes(n.Index)
Index = 0
For Each n1 In DepNode.Nodes
PId = findParentID(n1.Text)
For i = 0 To UBound(OD)
If PId = OD(i).Child_Id Then
DepNode.Nodes(Index).Nodes.Add(OD(i).Name & " - " & OD(i).Sector)
End If
Next
Index = Index + 1
Next
Next
然后我陷入困境,不确定如何迭代每个MainTeam以添加子团队,然后添加每个子团队下的子子团队.
我敢肯定有一种更简单的递归编码方式,但是我不知道怎么做.
I then get stuck and not sure how to iterate through each MainTeam to add the subteam and then the sub-subteams under each subteam.
I''m sure there is a simpler way to code this recursively but I cannot figure out how.
You help would be greatly appreciated.
推荐答案
首先,数据表中使用的名称令人困惑.我将其更改如下.
First the names used in the data table are confusing. I change them as follows.
Id Name Parent Id Type
1000 Company1 0 Company
1001 Function1 1000 Function
1002 Function1 1000 Function
1003 Function1 1000 Function
1004 Department1 1001 Department
1005 Department2 1001 Department
1006 Department3 1002 Department
1007 Department4 1003 Department
1008 MainTeam1 1004 Team
1009 MainTeam2 1004 Team
1010 MainTeam3 1005 Team
1011 MainTeam4 1005 Team
1012 MainTeam5 1006 Team
1013 MainTeam6 1007 Team
1014 Subteam1 1008 SubTeam
1015 Subteam2 1008 SubTeam
1016 Subteam3 1009 SubTeam
1017 Subteam4 1009 SubTeam
1018 Subteam5 1010 SubTeam
1019 Subteam6 1011 SubTeam
1020 Subteam7 1012 SubTeam
1021 Subteam8 1013 SubTeam
1022 Sub-Subteam1 1015 SubTeam
1023 Sub-Subteam2 1016 SubTeam
1024 Sub-Subteam2 1017 SubTeam
通过这种命名,解决方案变得更有意义.另请注意,Company1已获得父ID为0.
文章中的代码 [ ^ ]并非针对您的特定情况,但确实包含解决您的问题所需的逻辑.
这个想法是,您创建一个将给定父ID的所有条目添加到传递的Node
的函数,并对添加到TreeView
的所有子项递归调用该函数.
因此,您首先需要调用父ID 0和TreeView
中的第一个Node
的函数.然后,它添加第一个子Function1,然后再次调用自身以添加父ID 1001等的所有子元素.
我希望我已经足够清楚了.
With this naming the solution makes more sense. Also note that Company1 has gotten parent Id 0.
The code in this article[^] is not for your specific situation, but it does contain the logic need to solve your problem.
The idea is that you create a function that adds all the entries of the given Parent Id to a passed Node
and call the function recursively on all the child''s added to the TreeView
.
So you start by calling the function for Parent Id 0 and the first Node
in the TreeView
. It then add the first child Function1, after which it call itself again to add all child''s of parent Id 1001, etc.
I hope I made it clear enough.
我的解决方案如下,非常感谢AndréKraak的解决方案1,它使我正确地完成了代码.同时还要感谢wwwcoder.com的Patrick Santry提供了以上链接中的文章.
我的解决方案显然基于本文文章 [ ^ ],唯一的区别是我没有要使用的数据库,因此我将文件内容加载到复制文章的DataSet中.
My solution is as follows and many thanks to André Kraak for Solution 1 which set me on the right track to my finished code. Also thanks to Patrick Santry from wwwcoder.com for the article from the link above.
My solution is clearly based on this article, Article[^], the only difference is that I didn''t have a database to work with so I loaded the file contents into a DataSet replicating the article.
pt = rs.Tables(TableName)
TreeView1.Nodes.Clear()
TreeView1.Nodes.Add(New TreeNode("Root Tree Name"))
Dim tNode As New TreeNode
tNode = TreeView1.Nodes(0)
PopulateTreeView(Id of Parent, tNode, pt)
tNode.Expand()
pt声明为DataTable,第一行从DataSet中分配我需要的表.您需要添加第一个节点,即我的数据表中的Company1,然后需要传递根条目PopulateTreeView的ID,该ID进而成为子节点的父ID.在这种情况下,替换上面代码中的"Id或Parent"将是1000.
下面是递归地填充树视图的例程,与本文唯一的不同是,我传递了DataTable而不是每次运行都运行SQL查询,然后必须在DataTable中搜索父ID(即parentrow.Item). (2)并且,如果找到了我添加的节点,它将在例程结束时再次调用自身,该例程通过传递数据的ID递归地添加每个节点,该数据又又成为父ID.
pt is declared as a DataTable and the first line assigns the table I need from the DataSet. You need to add your first node which would be Company1 from my DataTable, you then need to pass the Id of your root entry PopulateTreeView which in turn becomes the Parent Id for your child nodes. In this case it would be 1000 replacing the ''Id or Parent'' in the above code.
The below is the routine which populates the treeview recursively, the only difference made from the article is that I pass the DataTable instead of running the SQL query everytime its run, I then have to search my DataTable for the Parent Id which is parentrow.Item(2) and if found I add the node, it calls itself again at the end of the routine which recursively adds each node by passing the Id of the data which in turn again becomes the Parent Id.
Private Sub PopulateTreeView(ByVal inParentID As Integer, ByRef inTreeNode As TreeNode, ByVal ParentTable As DataTable)
Dim parentrow As DataRow
For Each parentrow In ParentTable.Rows
If inParentID = parentrow.Item(2) Then
Dim parentnode As TreeNode
'we'll provide some text for the tree node.
Dim strLabel As String = parentrow.Item(1) & " (" & parentrow.Item(3) & ")"
parentnode = New TreeNode(strLabel)
inTreeNode.Nodes.Add(parentnode)
Application.DoEvents()
'set the tag property for the current node. This comes in useful if
'you want to pass the value of a specific record id.
'since the tag value is not visible, in the TreeView1_AfterSelect event
'you could pass the value to another sub routine, for example:
'FillDataGrid(TreeView1.SelectedNode.Tag)
parentnode.Tag = parentrow.Item(0)
'call the routine again to find childern of this record.
PopulateTreeView(parentrow.Item(0), parentnode, ParentTable)
End If
Next parentrow
End Sub
只要您知道您的ID和父ID是哪一列并将ID传递给例程,它就可以完美地处理不同类型的数据.
享受...
It works flawlessly and on different types of data as long as you know which columns your Id and Parent Id are and pass the Id to the routine.
Enjoy...
这篇关于数据列表到树状视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!