VB.NET还是比较常用的,于是我研究了一下VB.NET实现数据绑定,在这里拿出来和大家分享一下,希望对大家有用。以下介绍VB.NET实现数据绑定:

TextBox组件通过下列语句就可以把已经得到的数据集"myDataSet"中的"books.bookid"字段值绑定到TextBox1的"Text"属性上:TextBox1.DataBindings.Add ( New Binding ( "Text" , Me.myDataSet , "books.bookid" ) ) 了解了这二点,就不难实现对TextBox组件的数据绑定了。下面是VB.NET实现数据绑定的源程序代码:
- Imports System.Drawing
 - Imports System.Windows.Forms
 - Imports System.ComponentModel
 - Imports System
 - Imports System.Data.OleDb
 - Imports System.Data
 - Public Class Form1
 - Inherits Form
 - Private WithEvents Button1 As Button
 - Private TextBox1 As TextBox
 - Private myDataSet As DataSet
 - Private components As System.ComponentModel.Container
 - Public Sub New ( )
 - MyBase.New()
 - GetConnected ( )
 - InitializeComponent ( )
 - End Sub
 - '清除在程序中使用过的资源
 - Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
 - If disposing Then
 - If Not ( components Is Nothing ) Then
 - components.Dispose ( )
 - End If
 - End If
 - MyBase.Dispose ( disposing )
 - End Sub
 - '打开数据表,返回数据集
 - public Sub GetConnected ( )
 - '创建一个 OleDbConnection
 - Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\sample.mdb"
 - Dim myConn As OleDbConnection = new OleDbConnection ( )
 - myConn.ConnectionString = strCon
 - Dim strCom As string = " SELECT * FROM books "
 - '创建一个 DataSet
 - myDataSet = new DataSet( )
 - myConn.Open ( )
 - '用 OleDbDataAdapter 得到一个数据集
 - Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn )
 - '把Dataset绑定books数据表
 - myCommand.Fill ( myDataSet , "books" )
 - '关闭此OleDbConnection
 - myConn.Close ( )
 - End Sub
 - '初始化窗体中的组件
 - Private Sub InitializeComponent ( )
 - Me.Text = "对TextBox组件实现数据绑定!"
 - Me.Width = 400
 - Me.Height = 300
 - Button1 = New Button ( )
 - TextBox1 = New TextBox ( )
 - Button1.Left = 200
 - Button1.Top = 200
 - Button1.Width = 100
 - Button1.Height = 40
 - Button1.TabIndex = 0
 - Button1.Text = "数据绑定"
 - TextBox1.Left = 200
 - TextBox1.Top = 30
 - TextBox1.Width = 150
 - TextBox1.Height = 40
 - Me.Controls.Add ( Button1 )
 - Me.Controls.Add ( TextBox1 )
 - End Sub
 - Private Sub Button1_Click ( ByVal sender As Object , _
 - ByVal e As System.EventArgs ) Handles Button1.Click
 - TextBox1.DataBindings.Add ( New Binding ( "Text" , Me.myDataSet , "books.bookid" ) )
 - End Sub
 - End Class
 - Module Module1
 - Sub Main ( )
 - Application.Run ( new Form1 ( ) )
 - End sub
 - End Module