本篇文章给大家分享的是有关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
以上就是VB.NET中怎么实现数据绑定,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。