数据库结构:
id           name            code
1            中国             01
2            华东地区         0101
3            浙江省           010101
4            杭州市           01010101
5            温州市           01010102
6            乐清             0101010201怎么用递归算法加载到treeview 中 
另外,增加,删除 在树中怎么做的
重点是递归生成树的思想
在线等必须有代码才可以给分

解决方案 »

  1.   

    http://www.microsoft.com/china/community/Column/21.mspx参考这个你的这个结构最好再增加一个父ID的字段,会好处理一些
      

  2.   

    你只需要生成TREEVIEW吗?我做过类似的工作
    思路
    先增加 ROOT节点 , KEY 为 "R01"
    for i = 1 to 5
      n=4
      然后用SELECT语句把所有n位CODE的记录提取出来,
      插入父节点的KEY为"R" & mid(code,1,n-2) 后, 作为子节点,子节点的KEY 为 "R" & CODE
      n=n+2  
    next 
     
    我想这个思路,应该够了 :)
      

  3.   

    象楼主提供的数据, 不需要递归来实现, 如果是有父ID数据结构, 用递归,代码比较清晰
    比如 类似于传销组织的上下线关系, 只有一个头, 其他的成员都有一个上线, 而且有可能有N个下线
    这种TREEVIEW的构造,用递归是最好的了。
      

  4.   

    Private Sub LoadSubNodes(ByRef tvw As TreeView, Nd As Node, NodeID As Long)
      Dim Nd1 As Node
      Dim objDepts As New cdepts
      Dim i As Long
      objDepts.Find , NodeID '找到部门的所有子部门
      For i = 1 To objDepts.Count
        Set Nd1 = tvw.Nodes.Add(Nd, tvwChild, "A" & objDepts(i).ID, objDepts(i).DeptName, "D")
        Nd1.Expanded = True
        '递归加载下级部门.....
        LoadSubNodes tvw, Nd1, objDepts(i).ID
      Next i
    End Sub
      

  5.   

    '*************************************************************************
    '**模 块 名:clsTree
    '**说    明:YFHome 版权所有2004 - 2005(C)
    '**创 建 人:吴东雷
    '**日    期:2004-10-27
    '**修 改 人:
    '**日    期:
    '**描    述:树形结构
    '**版    本:V1.0.0
    '*************************************************************************
    Option Explicit
    Private cTableName As String    '表名
    Private cColID As String        '编号字段名
    Private cColName As String      '此结点名称
    Private cColParent As String    '父字段名称
    Private cstrSql As String
    Private cRst As ADODB.Recordset
    Private cNode As Node
    Public strError As String       '保存错误信息
    'Public Enum RelationShip        '增加结点的级别
    '    tvwFirst = 0
    '    tvwLast = 1
    '    tvwNext = 2
    '    tvwPrevious = 3
    '    tvwChild = 4
    'End Enum'表名
    Public Property Get TableName() As String
        TableName = cTableName
    End PropertyPublic Property Let TableName(ByVal vNewValue As String)
        cTableName = vNewValue
    End Property
    '编号字段名
    Public Property Get ColID() As String
        ColID = cColID
    End PropertyPublic Property Let ColID(ByVal vNewValue As String)
        cColID = vNewValue
    End Property'结点自身名称
    Public Property Get ColName() As String
       ColName = cColName
    End PropertyPublic Property Let ColName(ByVal vNewValue As String)
        cColName = vNewValue
    End Property'父列字段名称
    Public Property Get ColParent() As String
       ColParent = cColParent
    End PropertyPublic Property Let ColParent(ByVal vNewValue As String)
        cColParent = vNewValue
    End Property'测试类中信息是否完整
    Private Function cTestTblInfo() As Boolean
        cTestTblInfo = False
        If cTableName = "" Then
            strError = "表名不能为空!"
            Exit Function
        End If
        If cColID = "" Then
            strError = "编号字段名称不能为空!"
            Exit Function
        End If
        If cColName = "" Then
            strError = "结点名称字段名不能为空!"
            Exit Function
        End If
        If cColParent = "" Then
            strError = "父结点标识字段名不能为空!"
            Exit Function
        End If
        cTestTblInfo = True
    End Function
    '递归方法,添充一级以下的结点
    Private Sub cMakeNode(oTree As MSComctlLib.TreeView, strParentKey As String)
        Dim tmpRst As New ADODB.Recordset
        cstrSql = "select " & cColID & "," & cColName & "," & cColParent & _
                    " from " & cTableName & " where " & cColParent & "=" & Mid$(strParentKey, 2)
        Set tmpRst = ExecuteSQL(cstrSql)
        Do While Not tmpRst.EOF
            Set cNode = oTree.Nodes.Add(strParentKey, tvwChild, "N" & tmpRst.Fields(cColID), tmpRst.Fields(cColName), 2, 3)
            cNode.Tag = strParentKey
            Call cMakeNode(oTree, cNode.Key)
            tmpRst.MoveNext
        Loop
    End Sub'*************************************************************************
    '**函 数 名:MakeTree
    '**输    入:oTree(MSComctlLib.TreeView) -目标TreeView控件
    '**        :                            -起始父结点标识号,为0,则新增结点为顶
    '**        :级结点,为其它数,则以此数为根结点进行生成树形
    '**输    出:(Boolean) -成功,true;失败,false
    '**功能描述:生成树形
    '**全局变量:
    '**调用模块:
    '**作    者:吴东雷
    '**日    期:2004-10-27
    '**修 改 人:
    '**日    期:
    '**版    本:V1.0.0
    '*************************************************************************
    Public Function MakeTree(oTree As MSComctlLib.TreeView, _
                            intParentIndex As Integer) As Boolean
        MakeTree = False
        On Error GoTo errHandle:
        If cTestTblInfo = False Then
            Err.Raise vbObjectError + 513, "clsTree", strError
            Exit Function
        Else
            cstrSql = "select " & cColID & "," & cColName & "," & cColParent & _
                    " from " & cTableName & " where " & cColParent & " is null"
            Set cRst = ExecuteSQL(cstrSql)
            Do While Not cRst.EOF
                If intParentIndex = 0 Then
                    Set cNode = oTree.Nodes.Add(, , "N" & cRst.Fields(cColID), cRst.Fields(cColName), 2, 3)
                Else
                    Set cNode = oTree.Nodes.Add(intParentIndex, tvwChild, "N" & cRst.Fields(cColID), cRst.Fields(cColName), 2, 3)
                End If
                cNode.Tag = UCase("Null")          'tag标记保存此结点的父结点ID,为NULL,则说明此结点为根结点
                Call cMakeNode(oTree, cNode.Key)
                cRst.MoveNext
            Loop
        End If
        MakeTree = True
        Exit Function
    errHandle:
        Me.strError = Err.Description
        MakeTree = False
    End Function
    '增加一个结点
    'otree:树形控件
    'NodeKey:关键结点索引
    'RS:与NodeKey的关系
    'selfText:显示值
    'selfKey:键值
    Public Function AddNewNode(oTree As MSComctlLib.TreeView, _
                                NodeKey As Integer, _
                                rs As RelationShip, _
                                selfText As String, _
                                selfKey As String) As Boolean
        AddNewNode = False
        On Error GoTo errHandle:
        Set cNode = oTree.Nodes.Add(NodeKey, rs, "N" & selfKey, selfText, 2, 3)
        If rs <> tvwChild Then      '同级则将Tag与此Node相同
            cNode.Tag = oTree.Nodes(NodeKey).Tag
        ElseIf rs = tvwChild Then   '如果是下级,则将Tag设成父Node的Key
            cNode.Tag = oTree.Nodes(NodeKey).Key
        End If
        AddNewNode = True
        Exit Function
    errHandle:
        Me.strError = Err.Description
        AddNewNode = False
    End Function
    '删除一个结点
    Public Function DelNode(oTree As MSComctlLib.TreeView, _
                            selfKey As String) As Boolean
        DelNode = False
        On Error GoTo errHandle:
        oTree.Nodes.Remove (selfKey)
        DelNode = True
        Exit Function
    errHandle:
        strError = Err.Description
        DelNode = False
    End Function
    '对一个结点进行改名
    Public Function RenameNode(oTree As MSComctlLib.TreeView, _
                                selfKey As String, _
                                newText As String) As Boolean
        RenameNode = False
        On Error GoTo errHandle:
        Set cNode = oTree.Nodes(selfKey)
        cNode.Text = newText
        RenameNode = True
        Exit Function
    errHandle:
        strError = Err.Description
        RenameNode = False
    End FunctionPrivate Sub Class_Initialize()
        Set cRst = New ADODB.Recordset
    End Sub我写的一个类..
    你试一下.
      

  6.   

    你那个不需要用递归。对记录集的代码进行排序后,就象上面的样子,
    就闭着眼添加就行了。条件需要如下:
    1、代码排序后的记录集。
    2、需要用代码做KEY
    3、确定谁是第一级。