VB中,怎么将TreeView绑定到数据库?
例如图TreeView结构,我需要的功能如下:数据库abc.mdb中,有PJ表,表中有字段BRAND和MODEL,分别为品牌和型号。在读取进入窗口时,自动将PJ表中的BR...
例如图TreeView结构,我需要的功能如下:
数据库 abc.mdb中,有PJ表,表中有字段BRAND和MODEL,分别为品牌和型号。
在读取进入窗口时,自动将PJ表中的BRAND作为根节点,将MODEL作为子节点。
点击 刷新 commad1 可刷新TreeView,清空暂时不做功能。
然后,左侧有两个text,当点击右侧的TreeView的某子节点时,自动读取数据库中该型号的相关参数到左侧Text中,同时,右侧的这个TreeView能否添加右键功能,例如“删除”,右键某子节点,点击删除后,将清除当前数据库这个型号的一整行。
求高人相助,100悬赏奉上。。。
以下是我修改后的代码。
Private Sub DelRecord(ByVal id As String)
Dim i As Integer
Dim Rs As ADODB.Recordset
Set Rs = getrs(sql, "ideashome")
Cnn.ConnectionString = ConnStr
sql = "Delete * From [PJ] where [MODEL]='" & Text47.Text & "'"
TreeView1.Nodes.Clear
If Rs.RecordCount > 0 Then
For i = 1 To Rs.RecordCount
TreeView1.Nodes.Add , , "R" & i, Rs.Fields("BRAND")
TreeView1.Nodes.Add "R" & i, tvwChild, , Rs.Fields("MODEL")
Rs.MoveNext
Next
Rs.Close
End If
Set Rs = Nothing
End Sub 展开
数据库 abc.mdb中,有PJ表,表中有字段BRAND和MODEL,分别为品牌和型号。
在读取进入窗口时,自动将PJ表中的BRAND作为根节点,将MODEL作为子节点。
点击 刷新 commad1 可刷新TreeView,清空暂时不做功能。
然后,左侧有两个text,当点击右侧的TreeView的某子节点时,自动读取数据库中该型号的相关参数到左侧Text中,同时,右侧的这个TreeView能否添加右键功能,例如“删除”,右键某子节点,点击删除后,将清除当前数据库这个型号的一整行。
求高人相助,100悬赏奉上。。。
以下是我修改后的代码。
Private Sub DelRecord(ByVal id As String)
Dim i As Integer
Dim Rs As ADODB.Recordset
Set Rs = getrs(sql, "ideashome")
Cnn.ConnectionString = ConnStr
sql = "Delete * From [PJ] where [MODEL]='" & Text47.Text & "'"
TreeView1.Nodes.Clear
If Rs.RecordCount > 0 Then
For i = 1 To Rs.RecordCount
TreeView1.Nodes.Add , , "R" & i, Rs.Fields("BRAND")
TreeView1.Nodes.Add "R" & i, tvwChild, , Rs.Fields("MODEL")
Rs.MoveNext
Next
Rs.Close
End If
Set Rs = Nothing
End Sub 展开
3个回答
展开全部
'VB利用ADODB连接ACCESS数据库,执行查询、删除操作
'引用Microsoft ActiveX Data Objects 2.8 Library
Dim select_id As String
Dim ConnStr As String
Private Sub Command1_Click() '刷新
GetValue
End Sub
Private Sub Form_Load()
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\你文件的路径\abc.mdb;Persist Security Info=False"
TreeView1.LineStyle = tvwRootLines
TreeView1.LabelEdit = tvwManual
GetValue
End Sub
Private Sub mnu_del_Click() '菜单:删除
DelRecord select_id
End Sub
Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim nd As Node
If Button = vbRightButton Then
Set nd = TreeView1.HitTest(x, y)
If Not nd Is Nothing Then
If Left(nd.Key, 1) = "R" Then
select_id = nd.Text
Me.PopupMenu mnupop
End If
End If
End If
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
If Left(Node.Key, 1) = "R" Then
Text1.Text = Node.Text
Text2.Text = Node.Child.Text
End If
End Sub
Private Sub GetValue()
Dim i As Integer
Dim Cnn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Cnn_c As New ADODB.Command
Set Cnn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Cnn.ConnectionString = ConnStr
With Cnn
.CursorLocation = adUseClient
.Open
End With
Cnn_c.CommandType = adCmdText
Cnn_c.CommandText = "select * from PJ"
Set Cnn_c.ActiveConnection = Cnn
Set Rs = Cnn_c.Execute
TreeView1.Nodes.Clear
If Rs.RecordCount > 0 Then
For i = 1 To Rs.RecordCount
TreeView1.Nodes.Add , , "R" & i, Rs.Fields("BRAND")
TreeView1.Nodes.Add "R" & i, tvwChild, , Rs.Fields("MODEL")
Rs.MoveNext
Next
Rs.Close
End If
Cnn.Close
Set Cnn_c = Nothing
Set Rs = Nothing
Set Cnn = Nothing
End Sub
Private Sub DelRecord(ByVal id As String)
Dim i As Integer
Dim Cnn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Cnn_c As New ADODB.Command
Set Cnn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Cnn.ConnectionString = ConnStr
With Cnn
.CursorLocation = adUseClient
.Open
End With
Cnn_c.CommandType = adCmdText
Cnn_c.CommandText = "delete from PJ where BRAND='" & id & "'"
Set Cnn_c.ActiveConnection = Cnn
Set Rs = Cnn_c.Execute '执行删除
Cnn_c.CommandText = "select * from PJ"
Set Rs = Cnn_c.Execute '执行查询
TreeView1.Nodes.Clear
If Rs.RecordCount > 0 Then
For i = 1 To Rs.RecordCount
TreeView1.Nodes.Add , , "R" & i, Rs.Fields("BRAND")
TreeView1.Nodes.Add "R" & i, tvwChild, , Rs.Fields("MODEL")
Rs.MoveNext
Next
Rs.Close
End If
Cnn.Close
Set Cnn_c = Nothing
Set Rs = Nothing
Set Cnn = Nothing
End Sub
展开全部
搜索万能的网络,找到一条链接:http://bbs.csdn.net/topics/370135466其中稍有介绍。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用一个gridview 等控件绑定
用timer进行实时更新
更多追问追答
追问
我需要的是代码,不是想法,我对VB不懂,只是想做一个玩玩。
追答
不懂还能做? 你是想玩我吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询