我正在尝试构建一个使用mysql数据库中的数据填充的树视图。 通常我会说这不会有问题,除非我允许用户控制root,parent和child元素(数量可能会有所不同,命名也可能不稳定)
示例:假设数据库显示员工 - 但用户拥有多个具有多个部门的公司(他可以添加或删除)如下所示
Company ABC
Accounting
Employee A
Employee B
Marketing
Employee D
Employee F
Operations
Employee Z
Human Resources
Employee N
Company 123
Road Crew
Employee 1
Employee 2
Employee 3
Dispatcher
Employee 5
我提出的方法是执行running for语句并确定项目的类别(root,parent或child)。然后运行一个语句,将其适当地添加到treeview,rootitem或parentitem。
Dim con As New MySqlConnection()
Dim adptr As New MySqlDataAdapter
Dim pagers As New DataTable
Dim Roots() As TreeNode
Dim Parents() As TreeNode
Dim Children() As TreeNode
con.ConnectionString = "server=localhost;" _
& "user id=*****;" _
& "password=****;" _
& "database=****"
adptr = New MySqlDataAdapter("SELECT * FROM pagers", con)
Try
adptr.Fill(pagers)
Catch err As Exception
Dim strError As String = "Exception: & err.ToString()"
End Try
If pagers.Rows.Count > 0 Then
For pop As Integer = 0 To pagers.Rows.Count - 1
If pendrun.Rows(pop)("type") = "root" Then
Dim curid As Integer = pendrun.Rows(pop)("id")
Dim roots(curid) As TreeNode = TreeView1.Nodes.Add(pendrun.Rows(pop)("name"))
End If
If pendrun.Rows(pop)("type") = "parent" Then
Dim curid As Integer = pendrun.Rows(pop)("id")
Dim rootid As Integer = pendrun.Rows(pop)("rootid")
Dim Parents(curid) As TreeNode = Roots(rootid).Nodes.Add(pendrun.Rows(pop)("name"))
End If
If pendrun.Rows(pop)("type") = "child" Then
Dim curid As Integer = pendrun.Rows(pop)("id")
Dim parentid As Integer = pendrun.Rows(pop)("parentid")
Dim Children(curid) As TreeNode = Parents(parentid).Nodes.Add(pendrun.Rows(pop)("name"))
End If
Next
End If
这个
有两个明显的问题问题#1是软件可能在创建父级之前到达子条目,这可能会产生错误
可能的解决方案#1 - 我可以分别调用mysql数据库3并使用While type =“root”then =“parent”then =“child”,这实际上并不理想。
问题#2是VB.net不允许我创建一个treenode数组,至少在分配我的方式时不会。
可能的解决方案#2 - 我不知道?
我确信这是以前遇到过的问题 - 是否有人知道更合适的方法来解决这个问题
表示例:
id | name | type | rootid | parentid
1 Company ABC root Null Null
2 Accounting parent 1 Null
3 Employee A child 1 2
4 Employee B child 1 2
5 Marketing parent 1 Null
6 Employee D child 1 5
7 Employee F child 1 5
8 Marketing parent 1 Null
...
如果你愿意,我会继续建立更多的表格
检索代码:
con.ConnectionString = "server=****;" _
& "user id=****;" _
& "password=****;" _
& "database=***"
adptr = New MySqlDataAdapter("SELECT * FROM pagers", con)
Try
adptr.Fill(pagers)
pagers.PrimaryKey = New DataColumn() {pagers.Columns("id")}
Catch err As Exception
Dim strError As String = "Exception: & err.ToString()"
End Try
If pagers.Rows.Count > 0 Then
For pop As Integer = 0 To pagers.Rows.Count - 1
If pagers.Rows(pop)("type") = "root" Then
LoadRoot(Nothing, pagers.Rows(pop)("id"))
End If
Next
End If
答案 0 :(得分:1)
我无法在评论中说明这一点。 我填充表格略有不同,使用parentid列来控制树视图的布局:
8,“营销”,“父母”,1,1
9,“公司123”,“根”,Null,Null
然后将所需Root的id传递给此函数:
Private Sub LoadRoot(ByVal Par As TreeNode,ByVal Id As Integer) Dim oNode As TreeNode Dim oRow As DataRow
oRow = mTbl.Rows.Find(New Object() {Id})
If Not oRow Is Nothing Then
If Par Is Nothing Then
oNode = tv.Nodes.Add(oRow.Item("name").ToString)
Else
oNode = Par.Nodes.Add(oRow.Item("name").ToString)
End If
For Each oRow In mTbl.Select("parentid = " & Id.ToString)
LoadRoot(oNode, CInt(oRow.Item("id")))
Next
End If
End Sub