winform的treeview怎么进行数据库绑定。

winform的treeview怎么进行数据库绑定。,第1张

treeview没有DataSource属性,需要通过添加节点,如用:treeView1.Nodes.Add(nodes)添加,nodes参数,便可从数据库中读取某个字段信息来填充了。

比如:

//数据库连接,用了helper类

string commandText="select NAME from STUDENT_TB"

dt = OracleHelper.GetDataTable(commandText)

//treeview添加节点数据

string nodes=string.Empty

for(int i=0i<dt.Rows.Counti++)

{

nodes=dt.Rows[i]["NAME"].ToString()

TreeNode root = new TreeNode(nodes)

root.Tag = 0

treeView1.Nodes.Add(root)

}

'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.0Data Source=C:\你文件的路径\abc.mdbPersist 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

1.设置两个DataSet来缓存从SQL读出来的表。

2.如果两个表是Inner Join关系,那好说:把表连起来度就可以了,如果不是Inner Join关系,那就慢慢扫描吧,把学院表的值加入TreeView中,在扫描专业表,每扫到一个专业,检查TreeView.Nodes.Text,如果学院一致,.Node.Add加专业就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9378823.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存