我想尝试构建一个使用mysql数据库中的数据填充的treeview。 通常我会说这是没有问题的,除了我允许用户控制根,父母和子元素(数量可能会变化,命名可能不稳定)从mysql内容分层填充treeview
示例:假设数据库显示员工 - 但用户拥有多家公司有多个部门(他可以添加或删除)就像我想出了这个以下
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
的方法是做一个运行了声明,并确定该项目是什么类别(根,父母,或儿童)。然后运行一条语句将其添加到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 - 我可以分3次调用mysql数据库,并使用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
在你有多少表保存的数据?? –
Plz,提供表格和有架构.. –
表格示例已被添加到问题 –