回 帖 发 新 帖 刷新版面

主题:有关于TreeView的问题

到目前为止,我一直在逃避这个事实,做程序的时候一直避免使用到TreeView。[em8]
希望各位朋友能够给些指点。

到目前为止,我只能写出界面上的代码,无法添加到后台数据库中去,如下:
这个是想做一个部门级别的树

Private Sub CmdNewDepa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdNewDepa.Click
        Dim _node As TreeNode
        If Not Me.txtDepa.Text.Trim.Equals("") Then
            _node = New TreeNode(Me.txtDepa.Text.Trim)
            Me.TreeView1.Nodes.Add(_node)
        End If
        txtDepa.Text = ""
    End Sub

    Private Sub CmdNewDepaChild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdNewDepaChild.Click
        Dim _node As TreeNode
        If Not Me.txtChildDepa.Text.Trim.Equals("") Then
            If Not Me.TreeView1.SelectedNode Is Nothing Then
                Me.TreeView1.SelectedNode.Nodes.Add(Me.txtChildDepa.Text.Trim)
            Else
                Me.TreeView1.Nodes.Add(Me.txtChildDepa.Text.Trim)
            End If
        End If
        txtChildDepa.Text = ""
    End Sub

我希望能够动态添加到数据库中去,怎么弄??
另外,数据库的结构应该是怎么样的,
一级部门     二级部门    三级部门
a            b           c
a            b           d

还是应该
一级部门     二级部门    三级部门
a            b           c
Null         Null        d


或者说还有其他的结构,请大家指教

回复列表 (共12个回复)

11 楼

终于搞定了[em4][em4][em4][em4][em4][em4][em4][em4][em4][em4][em4][em4][em4]
谢谢大家的帮忙了,我快要疯掉了,[em65]休息休息一下。[em66]
代码如下:通用性应该还算可以的……  表结构参考10楼
Dim mydataset As New DataSet
Private m_TopNode As TreeNode

Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   sqlstr = "select * from department"
   conn = New SqlConnection(connstr)
   conn.Open()
   myadapter = New SqlDataAdapter(sqlstr, conn)
   myadapter.Fill(mydataset, "depart")
   conn.Close()

   Dim str1, str2 As String
   Dim _nodes As TreeNode
   Dim j As Integer

   For j = 0 To mydataset.Tables("depart").Rows.Count - 1
       If mydataset.Tables("depart").Rows(j).Item("P_Cloumn").Equals("") Then
          m_TopNode = New TreeNode(mydataset.Tables("depart").Rows(j).Item("C_Cloumn").ToString)
          m_TopNode.Tag = mydataset.Tables("depart").Rows(j).Item("C_Cloumn").ToString
          Me.TreeView1.Nodes.Add(m_TopNode)

     Dim i As Integer
         For i = 0 To mydataset.Tables("depart").Rows.Count - 1
           str1 = mydataset.Tables("depart").Rows(i).Item("P_Cloumn").ToString
           str2 = mydataset.Tables("depart").Rows(i).Item("C_Cloumn").ToString

           If Not str1.Equals("") Then
               If str1.Equals(mydataset.Tables("depart").Rows(j).Item("C_Cloumn").ToString) Then
                  _nodes = New TreeNode(str2)
                  _nodes.Tag = str2
                  _nodes = Fill_TreeView(_nodes, str2)
                  m_TopNode.Nodes.Add(_nodes)
                End If
           End If
         Next
       End If
     Next
Me.TreeView1.ExpandAll()
End Sub

    Public Function Fill_TreeView(ByVal _node As TreeNode, ByVal strId As String) As TreeNode
        Dim i As Integer
        Dim _nodes As TreeNode
        Dim str1, str2 As String

        For i = 0 To mydataset.Tables("depart").Rows.Count - 1
            str1 = mydataset.Tables("depart").Rows(i).Item("P_Cloumn").ToString
            str2 = mydataset.Tables("depart").Rows(i).Item("C_Cloumn").ToString
            If Not str1.Equals("") Then
                If str1.Equals(strId) Then
                    _nodes = New TreeNode(str2)
                    _nodes.Tag = str2
                    _nodes = Fill_TreeView(_nodes, str2)
                    _node.Nodes.Add(_nodes)
                End If
            End If
        Next
        Return _node
    End Function

12 楼

这是我在做一个项目时用到的.不知道是不是这个意思!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myconnection.ConnectionString = "server=hlr;database=bluehill;user ID=hufox;password=;"
        mycommand.CommandText = "SELECT DeptID,DeptName FROM tblDepartment"
        mycommand.Connection = myconnection

        Try

            myconnection.Close()

            myconnection.Open()

            Dim mysqlreader As SqlClient.SqlDataReader = mycommand.ExecuteReader

            tvDept.Nodes.Clear() '将目录树清空

            While mysqlreader.Read()

                Dim tree_root As New TreeNode

                tree_root.Tag = mysqlreader.GetInt32(0) '把编号放入tag中

                tree_root.Text = mysqlreader.GetString(1) '树上显示的是根节点名称

                '请根据你数据库字段的类型来决定是否用getstring或其它类型

                tvDept.Nodes.Add(tree_root)

            End While

        Catch ex As Exception

            MessageBox.Show(ex.ToString, "数据表根节点载入错误", vbOKOnly)

        Finally

            myconnection.Close()

        End Try

        tvDept.ExpandAll()

        tvDept.Select()


    End Sub
    Public Function NodeLevel(ByVal n As TreeNode) As Byte

        '* 找出树中当前节点的级数

        Dim i As Byte = 1

        Dim m As String

        Do Until n.Parent Is Nothing

            n = n.Parent

            i += 1

        Loop

        Return i

    End Function


    Public Sub fill_treeleaf()

        mycommand.Connection = myconnection

        Try

            myconnection.Open()

            Dim mysqlreader As SqlClient.SqlDataReader = mycommand.ExecuteReader

            While mysqlreader.Read()

                Dim tree_leaf As New TreeNode

                tree_leaf.Tag = mysqlreader.GetString(0)

                tree_leaf.Text = mysqlreader.GetString(0)

                tvDept.SelectedNode.Nodes.Add(tree_leaf)

            End While

        Catch ex As Exception

            MsgBox(ex.Message)

        Finally

            myconnection.Close()

        End Try

    End Sub



    Private Sub tvDept_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvDept.AfterSelect

        If NodeLevel(e.Node).ToString = "1" Then

            If e.Node.GetNodeCount(False) = 0 Then


                mycommand.CommandText = "select Name from tblEmployee  where DeptID  = " & e.Node.Tag & " "

                fill_treeleaf()

            End If

        End If
       


    End Sub

    '在调用展开之前这个函数,在其中加入折叠原来展开的目录
    Private Sub tvDept_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvDept.BeforeExpand
        Static lastNode As TreeNode = e.Node
        lastNode.Collapse()
        lastNode = e.Node

    End Sub

我来回复

您尚未登录,请登录后再回复。点此登录或注册