vb中treeview 与SQL数据库如何绑定
我有一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
在网上查了不相关解决方法,但学得实在有限,无法解决。
请提供代码,多谢了!
[解决办法]
自己的代码大概改了下,没测试,思路是这样,你自己测试吧
- VB code
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.CloseSet prs = Nothingrs.MoveNext Loop rs.CloseSet rs = NothingRoot.Expanded = TruePrivate 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 = rsEnd Function