注册 登录
编程论坛 VB.NET论坛

求助:treeview和DataGridView数据联动

jason7275 发布于 2022-02-12 22:14, 1422 次点击
求助:treeview和DataGridView数据联动
Form1中有两个控件:treeview1和DataGridView1,treeview1绑定“单位信息”表,DataGridView 1绑定“职工信息”表。

单位信息表如下:

编码            单位名称
DY101        龙塘镇中心小学
DY102        龙塘镇第二小学
DY103        龙塘镇井岭小学
……

职工信息表如下:

单位编码        职工编码        姓名        性别        身份证号
DY101        DY1010001        郑小平    男        441802000000000000
DY102        DY1020001        何凯婷    女        441802000000000000
……
选择treeview1的一个节点(单位)时,在DataGridView1中显示本节点(单位)的职工
(以下代码这个功能还未实现,求高手指点)

Imports System.Data.OleDb
Imports System.
Imports
Imports System.Text

Public Class Form1
    Dim conn, conntree As New OleDbConnection
    Dim da, treeda As New OleDbDataAdapter
    Dim ds, treeds As New DataSet
    Dim dr, treedr As OleDbDataReader
    Dim dt, tredr As New DataTable()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Try
            Dim sqltree As String
            sqltree = "select * from 单位信息 order by 编码"
            conntree.ConnectionString = " provider = Microsoft.ACE.OLEDB.12.0; Data Source = " & Application.StartupPath & "/staffmis.mdb"
            conntree.Open()
            Dim comtree As New OleDbCommand(sqltree, conntree)
            treedr = comtree.ExecuteReader()
            TreeView1.Nodes.Clear()

            Dim tree_root As New TreeNode()

            '添加根节点
            tree_root.Tag = "DY"
            tree_root.Text = "所有单位"
            TreeView1.Nodes.Add(tree_root)

            '添加二级节点,从单位信息表中获取单位名称作为二级节点
            While treedr.Read()
                Dim root As New TreeNode()
                root.Tag = treedr.GetValue(0)
                root.Text = treedr.GetValue(1)
                TreeView1.Nodes(0).Nodes.Add(root)
            End While

            conntree.Close()
            TreeView1.ExpandAll()
            TreeView1.Select()

        Catch
            MessageBox.Show("单位信息数据库连接失败!")

        End Try
        Try

            Dim sql As String
            sql = "select * from 职工信息 order by 职工编码"
            conn.ConnectionString = " provider = Microsoft.ACE.OLEDB.12.0; Data Source = " & Application.StartupPath & "/staffmis.mdb"
            conn.Open()
            Dim com As New OleDbCommand(sql, conn)
            dr = com.ExecuteReader()
            dt.Load(dr)
            DataGridView1.DataSource = dt
            conn.Close()


        Catch
            MessageBox.Show("职工信息数据库连接失败!")
        End Try

        With Me.DataGridView1
            .ReadOnly = True
            .AllowUserToAddRows = False
            .Columns(0).Width = 80
            .Columns(1).Width = 80
            .Columns(2).Width = 130
            .Columns(3).Width = 70
            .Columns(4).Width = 55
            .Columns(5).Width = 130
            .Columns(6).Width = 80
            .Columns(7).Width = 60
            .Columns(8).Width = 80
            .Columns(9).Width = 80
            .Columns(10).Width = 80
            .Columns(11).Width = 80
            .Columns(12).Width = 150
            .Columns(13).Width = 150
            .Columns(14).Width = 80
            .Columns(15).Width = 80
            .Columns(16).Width = 80
            .Columns(17).Width = 90
            .Columns(18).Width = 90
            .Columns(19).Width = 180
            .Columns(20).Visible = False

        End With

    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        With Me.DataGridView1
            .Width = Me.Width - 195
        End With
    End Sub
 
End Class

2 回复
#2
xyxcc1772022-02-27 14:32
了解bindingsource控件,可解决你的问题
#3
sssooosss2022-03-10 19:23
共同学习
1