十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇文章给大家分享的是有关VB.NET中怎么实现数据绑定,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联是一家专业提供金平企业网站建设,专注与成都做网站、成都网站建设、成都外贸网站建设、H5建站、小程序制作等业务。10年已为金平众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
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中怎么实现数据绑定,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。