我有一SQL数据表:表名为A,其字段为:fitemid int,fparentid int,fnumber var,fname var,其中,fparentid表示上一级的fitemid号。
举例:
fitemid      fparentid    fnumber     fname
  1            0            01         b
  2            0            02         c
  3            1            0101       ba
  4            1            0102       bb
  5            2            0201       ca
  6            2            0202       cb我想在VB中利用treeview做成菜单。效果如下:
  01   b
     0101  ba
     0102  bb
  02   c
     0201  ca
     0202  cb在网上查了不相关解决方法,但学得实在有限,无法解决。
请提供代码,多谢了!

解决方案 »

  1.   

    自己的代码大概改了下,没测试,思路是这样,你自己测试吧
    Set Root = trv.Nodes.Add(, , , "商品分类列表")
    strSql = "select * from A where fparentid=0"
    Set rs = OpenRecordset(strSql)
    Do While Not rs.EOF
        Set cnode = trv.Nodes.Add(Root, 4, , rs!fname)
        cnode.Tag = rs!fitemid
        '----------------------------二级分类
         strSql = "select * from A where fparentid = " & rs!fitemid
            Set prs = OpenRecordset(strSql)
            
            Do While Not prs.EOF
            
                Set pnode = trv.Nodes.Add(cnode, 4, , prs!fname)
                pnode.Tag = prs!fitemid
                
                prs.MoveNext
            Loop
            prs.Close
    Set prs = Nothing
    rs.MoveNext
        Loop
        rs.Close
    Set rs = Nothing
    Root.Expanded = True
    Private Function OpenRecordset(ByVal strSql As String) As ADODB.Recordset
        Dim rs As New ADODB.Recordset
        With rs
            .CursorLocation = adUseClient
            .CursorType = adOpenDynamic
            .Open strSql, conn, , , adCmdText
        End With
        Set OpenRecordset = rs
    End Function
      

  2.   

    yejian520的思路很好,值得借鉴,是还把它编成函数,再用递归算法来调用就能做成任意级次的呢?