Imports System.Data.OleDb  '引入命名空间
Public Class 例12_9
    Dim conn As OleDbConnection
    Dim ds As New DataSet()
    Dim da As OleDbDataAdapter
    Dim bmb As BindingManagerBase
    Private Sub OpenDataBase()
        Dim str As String = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source =f:\stu_manager.mdb" ' 定义连接字符串
        Dim selCmd As String = "Select * From stuinfo"
        conn = New OleDbConnection(str)    ' 创建OleDbConnection对象
        conn.Open()     ' 用Open方法打开数据库
        da = New OleDbDataAdapter(selCmd, conn)  ' 创建DataAdapter对象
        da.Fill(ds, "stuinfo")    ' 将记录集填充到ds中
    End Sub

    Private Sub fuzabinging_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OpenDataBase()
        ' 将两个文本框分别与数据集ds中的“stuinfo”表中的姓名与年龄字段绑定
        txtAge.DataBindings.Add("text", ds, "stuinfo.年龄")
        txtName.DataBindings.Add("text", ds, "stuinfo.姓名")
        ' 将DataGridView控件与数据集ds绑定
        DataGridView1.DataSource = ds.Tables("stuinfo")
        ' 创建BindingManagerBase对象实现控件同步
        bmb = Me.BindingContext(ds, "stuinfo")
    End Sub

    Private Sub fuzabinging_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        conn.Close()
        End
    End Sub

    Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
        '当单击 DataGridView的某一行时，文本框内容也定位到该行
        bmb.Position = DataGridView1.CurrentRow.Index
    End Sub
End Class