标题:求助:treeview和DataGridView数据联动
只看楼主
jason7275
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-2-12
 问题点数:0 回复次数:2 
求助:treeview和DataGridView数据联动
求助: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

搜索更多相关主题的帖子: New 信息 Dim 单位 End 
2022-02-12 22:14
xyxcc177
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:26
帖 子:197
专家分:1249
注 册:2017-7-8
得分:0 
了解bindingsource控件,可解决你的问题
2022-02-27 14:32
sssooosss
Rank: 9Rank: 9Rank: 9
等 级:禁止访问
威 望:3
帖 子:664
专家分:1115
注 册:2019-8-27
得分:0 
共同学习
2022-03-10 19:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-508334-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.205293 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved