2014-01-21 61 views
0

我想尝试构建一个使用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 
+0

在你有多少表保存的数据?? –

+0

Plz,提供表格和有架构.. –

+0

表格示例已被添加到问题 –

回答

1

我无法适应这个评论。 我填充表有点不同使用parentId的柱来控制树视图的布局:

  • 1, “ABC公司”, “根”,NULL,NULL
  • 2, “记帐”, “父”,1,1
  • 3, “员工A”, “孩子”,1,2
  • 4, “员工B”, “孩子”,1,2
  • 5中, “营销”, “parent”,1,
  • 6,“员工D”,“孩子”,1
  • 7, “员工F”, “孩子”,1,5
  • 8, “市场营销”, “父母”,1,1

  • 9中, “公司123”, “根”,NULL,空

  • 10 “路表”, “父母”,9,9
  • 11, “员工1”, “孩子”,9,10
  • 12, “员工2”, “孩子”, 9,10
  • 13“员工3”“孩子”9,10
  • 14“调度员”“父母”9,9
  • 15, “员工5”, “孩子”,9,14

然后所需的根的ID传递给该函数:

私人小组LoadRoot(BYVAL帕作为树节点,BYVAL标识作为整数) 昏暗oNode作为树节点 昏暗oRow作为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 
+0

可以说,用户然后决定删除#11“员工1”是会导致错误 –

+1

该示例仍然可以正常工作。如果我删除道路乘员组,那么它会在没有道路乘员组的情况下载入公司123,但有一些孤儿员工记录。树的加载看起来没问题,只需要控制添加和删除操作如何保持数据清洁。 – Lauren

+0

我想了解这里的代码 - 所以为了添加ID 11我会运行LoadRoot(???,11)什么?在添加根级别时,在插槽中的空值仅为空值 –