数据库结构如下:
ID        Sort
01        电脑
0101      联想
0102      海信
……
02        电视
0201      海信
0202      TCL
……
03        洗衣机
……
分类可能有很多级。小弟没有用过TreeView,请各位帮帮忙吧。

解决方案 »

  1.   

    '如果2个表分别存放产品类型和产品牌子
    Dim strSQL As String
    Dim sql As String
    Dim Rs As ADODB.Recordset
    Set Rs = New ADODB.Recordset
    Dim Rd As ADODB.Recordset
    Set Rd = New ADODB.RecordsetWith TreeView1
     .Nodes.Clear
     .Nodes.Add , , "NodBoot", "所有产品"
     strSQL = "select TypeID,ProductType from ProductType group by TypeID"
     If Rs.State = adStateOpen Then Rs.Close
     Rs.Open strSQL, gcnnData, adOpenDynamic, adLockOptimistic
     If Not Rs.BOF Then
      Rs.MoveFirst
      Do Until Rs.EOF
       .Nodes.Add "NodBoot", tvwChild, Rs!TypeID, Rs!ProductType
       sql = "select ProductID,ProductName from Product where ProductType='" & Rs!TypeID & "'"
       If Rd.State = adStateOpen Then Rd.Close
       Rd.Open sql, gcnnData, adOpenDynamic, adLockOptimistic
       If Not Rd.BOF Then
        Rd.MoveFirst
        Do Until Rd.EOF
         .Nodes.Add  Rs!TypeID, tvwChild,  Rd!ProductID, Rd!ProductName
         Rd.MoveNext
        Loop
       End If
       Rs.MoveNext
      Loop
     End If
     Rs.Close
     Set Rs = Nothing
     Rd.Close
     Set Rd = Nothing  
    End With