数据结构如下:
商品类别编号   商品类别名称   所属商品类别编号
    1            日化用品             0
    2              茶                 0
    3              化装品             1
    4              绿茶               2
    5              高级绿茶           4
    6              中级绿茶           4我要把这些类别按照树形结构在treeview中显示出来请大家帮帮忙

解决方案 »

  1.   

    Private Sub pvFillTree()    On Error GoTo vbErrorHandler    '
        ' Populate our TreeView Control with the Data from our Product database
        '
    Dim lCount As Long
        'Dim rsSections As DAO.Recordset
    Dim rsSections As ADODB.Recordset
    Dim sParent As String
    Dim sKey As String
    Dim sText As String
    Dim bBookMark As Boolean
    Dim nNode As Node
    Dim strSQL As String
    Dim sData() As String
    Dim lList As Long    strSQL = "Select * From tblProduct Order By tblProduct.所属商品类别编号"
        'Set rsSections = mDB.OpenRecordset(strSQL, dbOpenSnapshot)
        Set rsSections = New ADODB.Recordset
        With rsSections
            .Open strSQL, mADODBConnection, ADODB.adOpenKeyset, ADODB.adLockOptimistic
        End With    Set tvProduct.ImageList = Nothing
        Set tvProduct.ImageList = ImageList1    If rsSections.BOF And rsSections.EOF Then
            tvProduct.Nodes.Add , , "ROOT", "My Company", "VIEWBOOKMARKS"
            BoldTreeNode tvProduct.Nodes("ROOT")
            Exit Sub
        End If    TreeRedraw tvProduct.hWnd, False    rsSections.MoveFirst
        Set tvProduct.ImageList = Nothing
        Set tvProduct.ImageList = ImageList1
        '
        ' Populate the TreeView Nodes
        '    With tvProduct.Nodes
            .Clear
            .Add , , "ROOT", "My Company", "VIEWBOOKMARKS"
            '
            ' Make our Root Item BOLD
            '
            BoldTreeNode tvProduct.Nodes("ROOT")
            '
            ' Now add all nodes into TreeView, but under the root item.
            ' We reparent the nodes in the next step
            '
            lList = 1
            ReDim sData(1 To lList)        Do Until rsSections.EOF
                sParent = rsSections("所属商品类别编号").Value
                sKey = rsSections("商品类别编号").Value
                sText = rsSections("商品类别名称").Value            Set nNode = .Add("ROOT", tvwChild, "C" & sKey, sText, "FOLDER")
                '
                ' Record parent ID
                '
                lList = lList + 1            ReDim Preserve sData(1 To lList)
                sData(lList) = "C" & sParent
                'nNode.Tag = "C" & sParent
                rsSections.MoveNext
            Loop    End With
        '
        ' Here's where we rebuild the structure of the nodes
        '
        lList = 0
        For Each nNode In tvProduct.Nodes
            lList = lList + 1
            'sParent = nNode.Tag
            sParent = sData(lList)
            If Len(sParent) > 0 Then       
                If sParent = "C0" Then
                    sParent = "ROOT"
                End If
                Set nNode.Parent = tvProduct.Nodes(sParent)
            End If
        Next nNode
        '
        ' Now setup the images for each node in the treeview & set each node to
        ' be sorted if it has children
        '
        For Each nNode In tvProduct.Nodes
            If nNode.Children = 0 Then
                nNode.Image = "CHILD"
            Else
                nNode.Sorted = True
            End If
        Next nNode    rsSections.Close
        Set rsSections = Nothing    '
        ' Expand the Root Node
        '
        tvProduct.Nodes("ROOT").Sorted = True
        tvProduct.Nodes("ROOT").Expanded = True    TreeRedraw tvProduct.hWnd, TrueExit SubvbErrorHandler:    TreeRedraw tvProduct.hWnd, True    MsgBox Err.Number & " " & Err.Description & " " & Err.Source & " frmProduct::pvFillTree", , App.ProductNameEnd SubPrivate Sub TreeRedraw(ByVal lHwnd As Long, ByVal bRedraw As Boolean)'
    ' Utility Routine for TreeRedraw on/off
    '    SendMessageLong lHwnd, WM_SETREDRAW, bRedraw, 0End Sub
    Private Sub BoldTreeNode(nNode As Node)
        On Error GoTo vbErrorHandlerDim TVI As TVITEM
    Dim lRet As Long
    Dim hItemTV As Long
    Dim lHwnd As Long    Set tvProduct.SelectedItem = nNode    lHwnd = tvProduct.hWnd
        hItemTV = SendMessageLong(lHwnd, TVM_GETNEXTITEM, TVGN_CARET, 0&)    If hItemTV > 0 Then
            With TVI
                .hItem = hItemTV
                .mask = TVIF_STATE
                .stateMask = TVIS_BOLD
                lRet = SendMessageAny(lHwnd, TVM_GETITEM, 0&, TVI)
                .State = TVIS_BOLD
            End With
            lRet = SendMessageAny(lHwnd, TVM_SETITEM, 0&, TVI)
        End IfExit SubvbErrorHandler:
        MsgBox Err.Number & " " & Err.Description & " " & Err.Source, , "frmProduct::BoldTreeNode"End Sub