用TreeView控显示一个数据库里一张表的name字段,原代码以下!
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open "uio", conn, adOpenDynamic, adLockBatchOptimistic
rs.MoveFirst    '把光标放在表的第一行上.
Do Until rs.EOF   '一直找到最后一行
 CunZai = False
    J = Form1.TreeView1.Nodes.Count
    For i = 1 To Form1.TreeView1.Nodes.Count '检查新输入的父节点名称是否存在
 If Form1.TreeView1.Nodes.Count > 0 Then
   If Trim(rs("name")) = Form1.TreeView1.Nodes(i).Text Then CunZai = True
  End If
    Next i
    If CunZai = True Then '若存在, 则在父节点下建立子节点
 Set nodx = Form1.TreeView1.Nodes.add(Trim(rs("父点")), tvwChild, Trim(rs("子点")), Trim(rs("name")), 3)
Else '若不存在,则建立父节点和子节点
Set nodx = Form1.TreeView1.Nodes.add(, , Trim(rs("子点")), Trim(rs("name")), 1)
End If
Form1.TreeView1.Refresh
rs.MoveNext  '移动到下一行记录      
Loop
表内容
ID  父点  子点  name
27  a27c  c27c  电脑1
28  c27c  c28c  电脑2
29  c28c  c29c  电脑3
30  c28c  c30c  电脑4
31  c28c  c31c  电脑5
32  c28c  c32c  电脑6
33  c28c  c33c  电脑7
34  c28c  C34c  电脑8
就出现TreeView控件显示不对!显示以下.
|---电脑1
|
|---电脑2
|
|---电脑3
|
|---电脑4
|
|---电脑5
|
|---电脑6
|
|---电脑7
|
|---电脑8
表内容以下.
ID  父点  子点  name
27  a27c  c27c  电脑
28  c27c  c28c  电脑
29  c28c  c29c  电脑
30  c28c  c30c  电脑
31  c28c  c31c  电脑
32  c28c  c32c  电脑
33  c28c  c33c  电脑
34  c28c  C34c  电脑
TreeView控件显示就正确!显示以下.
|---电脑
|   |
|   |
|   ---电脑
|      |
|      |
|      ---电脑
|      |
|      | 
|      ---电脑
|      |
|      |
|      ---电脑
|      |
|      |
|      ---电脑
|      |
|      |
|      ---电脑
|---电脑  
也就是说name字段的内容全部相同,TreeView控件就显示正确,否则就TreeView就出现显示错误!
请各位帮助

解决方案 »

  1.   

    Trim(rs("name")) = Form1.TreeView1.Nodes(i).Text 
    估计是该句问题,判断名字相等可能节点位置有错
    电脑1<>电脑2  而电脑=电脑
    所以,将i改为i+1或i-1试试。
      

  2.   

    '*************************************************************************
    '**模 块 名: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用我这个类试试.
      

  3.   

    你是说把TreeView1.nodes(i).text的i改成i+1或i-1?
      

  4.   

    i 是一个nodes变量来的,所以不能改!
    而wumylove1234(毁于随) 的代码我又看不懂!能不能更简单的办法?
      

  5.   

    For i = 1 To Form1.TreeView1.Nodes.Count '检查新输入的父节点名称是否存在//你判断条件错了
                If Form1.TreeView1.Nodes.Count > 0 Then
                    If Trim(rs("父点")) = Form1.TreeView1.Nodes(i).Tag Then CunZai = True
                End If
            Next i
            If CunZai = True Then '若存在, 则在父节点下建立子节点
                Set nodx = Form1.TreeView1.Nodes.Add(Trim(rs("父点")), tvwChild, Trim(rs("子点")), Trim(rs("name")), 3)
                nodx.Tag = rs("子点")
             Else '若不存在,则建立父节点和子节点
                Set nodx = Form1.TreeView1.Nodes.Add(, , Trim(rs("子点")), Trim(rs("name")), 1)
                nodx.Tag = rs("子点")
            End If
      

  6.   

    多谢!danielinbiti(金) 
    不过我想问明白!
    If Trim(rs("父点")) = Form1.TreeView1.Nodes(i).Tag Then  的父点和tag
    nodx.Tag = rs("子点")
    是代表什么意思?
    特别是nodx.Tag = rs("子点")这一句真的想不到!@
      

  7.   

    因为收索有没有父节点是根据当前读取记录的父点内容和当前已经加载节点的值比较,所以加载节点时应该保存当前加载节点的值,而不是name
    Tag是很普遍用来记录变量的当然你的代码中Node.key应该就是节点的值吧