使用vb.net使用两个表填充树视图

时间:2016-03-17 10:21:48

标签: vb.net treeview

您好我有这两个表:

enter image description here

subDepartment table

然后我想使用treeview获得这个结果:

ID 部门 类别

  • 1个MIS系统 1 MIS网络 1名MIS程序员
  • 2审计业务 2审计DS
  • 3 HRD PA 3 HRD PayBen 3
    HRD PS 3 HRD PLD
  • 4 Acounting Sup 4会计FMCG
  • 5采购空白

或者像这样

  • MIS

-System
   -Networking
   -Programmer

  • AUDIT
    -operations
    -DS

  • HRD
    -PA
    -PayBen
    -PS
    -PLD

  • Acounting
    -Sup
    -FMCG

有人可以指导我,谢谢。我在互联网上找不到任何解决方案,而且我对vb.net语言不熟悉。

2 个答案:

答案 0 :(得分:0)

在下面的代码中,我给你逻辑。您只需要立即查询表格。

告诉我你是否需要解释。

    TreeView1.Nodes.Clear()

    'Create a first node and select it
    Dim root As TreeNode = TreeView1.Nodes.Add("Test")
    TreeView1.SelectedNode = root

    'Create two types of node
    Dim department As New TreeNode()
    Dim category As New TreeNode()

    'Daos (if you create a Department class and subDepartment class)
    Dim _daoD As New Departments(_cnx)
    Dim _daoSD As New subDepartments(_cnx)

    'Lists (depending on classes too)
    Dim listD As List(Of Department)
    Dim listSD As List(Of subDepatment)

    For Each dep As Department In listD
        'Add a Tree node for a new department
        department = New TreeNode(dep.department)
        department.Tag = dep.id
        root.Nodes.Add(department)
        TreeView1.SelectedNode = departement

        For Each subDep As subDepartment In listSubDep
            'Add a TreeNode for new categories
            categ = New TreeNode(subDep.category)
            categ.Tag = subDep.id
            Nodes.Add(categ)
        Next
    Next

然后,您可以创建4个类(第一个包含属性,第二个在表中查询)

Public Class Department 
    'properties
        Public Property id As Integer
        Get
            Return _ID
        End Get
        Set(ByVal Value As Integer)
            _ID = Value
        End Set
    End Property
   'etc
End Class

Public Class Departments
    Dim _cnx As OracleConnection (if you use Oracle)
    Public Sub New(ByVal pcnx As OracleConnection)
        _cnx = pcnx
    End Sub

   'Your queries
End Class

答案 1 :(得分:0)

这是我的解决方案。我希望这能帮助那些有这种问题的人。

Dim myConnString As String =“Data Source = name; Initial Catalog = database; Integrated Security = True”         Dim daMyName As New SqlDataAdapter         Dim dsMyName作为新数据集         Dim recCount As Integer         Dim mySelectQuery As String =“SELECT DEPARTMENT.department,subDept.category from subdept right join department on .department.id = subDept.parent”         将myConnection调暗为新的SqlConnection(myConnString)         将myCommand调暗为新的SqlCommand(mySelectQuery,myConnection)         myConnection.Open()         daMyName.SelectCommand = myCommand         daMyName.Fill(dsMyName)         Dim tbl As DataTable = dsMyName.Tables(0)         recCount = dsMyName.Tables(0).Rows.Count         tvw1.Nodes.Clear()'清除所有节点,这样我们就不会将树加载到树中

    Dim nd As TreeNode
    nd = tvw1.Nodes.Add("DEPARTMENT")
    Dim parentRow As DataRow

    Dim rowNdx As Integer
    rowNdx = 0
    daMyName.Fill(tbl)
    Dim Jan As String
    For Each parentRow In tbl.Rows
        If rowNdx = recCount Then
            Exit For
        End If
        Jan = dsMyName.Tables(0).Rows(rowNdx)("Department").ToString() 'set the next name
        Dim Dnode As TreeNode
        Dnode = tvw1.Nodes.Add(dsMyName.Tables(0).Rows(rowNdx)("department").ToString())
        Dim cnode As TreeNode
        cnode = New TreeNode
        Do While dsMyName.Tables(0).Rows(rowNdx)("department").ToString() = Jan And rowNdx < recCount
            'if it changes we need to kick out of Do While
            cnode = Dnode.Nodes.Add(dsMyName.Tables(0).Rows(rowNdx)("category").ToString())
            rowNdx = rowNdx + 1
            If rowNdx = recCount Then
                Exit Do
            End If
        Loop
    Next parentRow
    myConnection.Close()