TreeView控件问题,请高手指教!(急用,在线等待)

jeffzsy 2003-11-29 10:13:19
数据库结构如下:
商品部 商品组 商品类别
0001-家电部 0001-大家电组 0001-彩电
0001-家电部 0001-大家电组 0002-冰箱
0001-家电部 0002-小家电组 0003-电熨斗
0002-装饰部 0003-五金组 NULL
0003-商场部 NULL NULL

现在想用TreeView控件显示以上的类别关系。请问应该怎样做?谢谢
...全文
148 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2003-12-01
  • 打赏
  • 举报
回复
呵呵,太复杂化了,这样应该就可以了(没有调试):
Private Sub TreeViewInitialize()
On Error Resume Next
TvwProductionSort.Nodes.Clear
TvwProductionSort.Nodes.Add , , "root", "商品类别", "pic1"
'rst设置
rst.movefirst
Do While Not rst.EOF

If IsNull(rst!商品部) = False Then TvwProductionSort.Nodes.Add "root", 4, rst!商品部, rst!商品部, "pic2"
If IsNull(rst!商品组) = False Then TvwProductionSort.Nodes.Add CStr(rst!商品部), 4, CStr(rst!商品部) & rst!商品组, rst!商品组, "pic3"
If IsNull(rst!商品类别) = False Then TvwProductionSort.Nodes.Add CStr(rst!商品部) & rst!商品组, 4, CStr(rst!商品部) & rst!商品组 & rst!商品类别, rst!商品类别, "pic4"
rst.MoveNext
Loop
Set rst = Nothing
End Sub
snowflake310 2003-11-30
  • 打赏
  • 举报
回复
学习。
rainstormmaster 2003-11-30
  • 打赏
  • 举报
回复
呵呵,只能一条一条的读取
jeffzsy 2003-11-30
  • 打赏
  • 举报
回复
楼上的朋友,你现在给我的方法只考虑了现在的情况,要是数据库中的记录时动态变化的好像就不适用了吧?不过还是谢谢了!
NewHand11111 2003-11-30
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim tnode As Node

Set tnode = tree.Nodes.Add()
tnode.Text = "家电部"
tnode.Key = "jdb"
Set tnode = tree.Nodes.Add()
tnode.Text = "装饰部"
tnode.Key = "zsb"
Set tnode = tree.Nodes.Add()
tnode.Text = "商场部"
tnode.Key = "scb"
Set tnode = tree.Nodes.Add("jdb", tvwChild, , "大家电部")
tnode.Key = "djdb"
Set tnode = tree.Nodes.Add("jdb", tvwChild, , "小家电部")
tnode.Key = "xjdb"
Set tnode = tree.Nodes.Add("zsb", tvwChild, , "五金部")
Set g_rst = New ADODB.Recordset
strsql = "select * from view_productionsort"
g_rst.Open strsql, g_ado, adOpenKeyset, adLockReadOnly
While Not g_rst.EOF
If Trim(g_rst!商品组)= "大家电部" Then
Set tnode = tree.Nodes.Add("djdb", tvwChild, , Trim(g_rst!商品类别))
elseIf Trim(g_rst!商品组)= "小家电部" Then
Set tnode = tree.Nodes.Add("xjdb", tvwChild, , Trim(g_rst!商品类别))
End If
g_rst.MoveNext
Wend
End Sub

jeffzsy 2003-11-29
  • 打赏
  • 举报
回复
想了半天想出了一种实现方法,不过感觉太麻烦了,大家看看怎么能简单点,谢谢!!


Private Sub TreeViewInitialize()
Dim Str_TmpNode As String
Dim Str_TmpNode2 As String
TvwProductionSort.Nodes.Clear
TvwProductionSort.Nodes.Add , , "root", "商品类别", "pic1"
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
GetRecordset rst, "select * from view_productionsort"
If Not (rst.EOF And rst.BOF) Then
rst.MoveFirst
Str_TmpNode = ""
Str_TmpNode2 = ""
str_tmpnode3 = ""
Do Until rst.EOF
If Str_TmpNode <> rst!商品部 Then
TvwProductionSort.Nodes.Add "root", 4, rst!商品部, rst!商品部, "pic2"
If IsNull(rst!商品组) = False And Str_TmpNode2 <> rst!商品组 Then
TvwProductionSort.Nodes.Add CStr(rst!商品部), 4, rst!商品组, rst!商品组, "pic3"
Str_TmpNode2 = rst!商品组
If IsNull(rst!商品类别) = False And str_tmpnode3 <> rst!商品类别 Then
TvwProductionSort.Nodes.Add CStr(rst!商品组), 4, rst!商品类别, rst!商品类别, "pic3"
str_tmpnode3 = rst!商品类别
End If
Else
If IsNull(rst!商品类别) = False And str_tmpnode3 <> rst!商品类别 Then
TvwProductionSort.Nodes.Add CStr(rst!商品组), 4, rst!商品类别, rst!商品类别, "pic3"
str_tmpnode3 = rst!商品类别
End If
End If
Else
If IsNull(rst!商品组) = False And Str_TmpNode2 <> rst!商品组 Then
TvwProductionSort.Nodes.Add CStr(rst!商品部), 4, rst!商品组, rst!商品组, "pic3"
Str_TmpNode2 = rst!商品组
If IsNull(rst!商品类别) = False And str_tmpnode3 <> rst!商品类别 Then
TvwProductionSort.Nodes.Add CStr(rst!商品组), 4, rst!商品类别, rst!商品类别, "pic3"
str_tmpnode3 = rst!商品类别
End If
Else
If IsNull(rst!商品类别) = False And str_tmpnode3 <> rst!商品类别 Then
TvwProductionSort.Nodes.Add CStr(rst!商品组), 4, rst!商品类别, rst!商品类别, "pic3"
str_tmpnode3 = rst!商品类别
End If
End If
End If
Str_TmpNode = rst!商品部
rst.MoveNext
Loop
End If
Set rst = Nothing
End Sub

1,453

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧