myCommand.Fill(myDataSet, ″student″)
'关闭此OleDbConnection
myConn.Close()
End Sub
'初始化窗体中的组件
Private Sub InitializeComponent()
……(省略部分自动生成代码)
End Sub
'将各个文本框控件绑定到的各个字段。
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
TxtStuID.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuID″))
TxtStuName.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuName″))
TxtStuSex.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuSex″))
TxtStuBorn.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuBorn″))
TxtStuCore.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuCore″))
End Sub
End Class
Module Module1
Sub Main()
Application.Run(New Form1())
End SubEnd Module
编译后运行程序,看看是不是与上面介绍的实例运行结果相同。
在上面的代码中,我们新建了一个GetConnected()过程,用于建立连接,打开。在用代码实现数据绑定或者对进行任何操作前,就必须要先建立连接,打开,程序运行结束后再关闭数据连接。
在Button1_Click过程中,我们把“TextBox”控件绑定到数据集myDataSet中“student”的各个字段上。
程序运行后,点击“TextBox数据绑定”按纽,程序就会用myDataSet中“student”中的数据来自动填充文本框。
2.绑定到Label控件
有了上面的内容做基础,再来讨论如何把数据集绑定到Label控件上就显得很简单了。把数据绑定到Label控件的方法与绑定到TextBox控件的方法大同小异。此处不再详细讲解,只给出代码实现绑定到Label控件的核心部分代码。
Private Sub Button1_Click (ByVal sender As Object , _
ByVal e As System.EventArgs ) Handles Button1.Click
'把student表的StuName字段绑定到Label1的Text属性上。
Label1.DataBindings.Add ( New Binding ( ″Text″ , Me.myDataSet , ″student.StuName″ ) )
End Sub
3.绑定到ComboBox控件
上面介绍的是对组件的简单数据绑定,对组件的复杂数据绑定和它有所区别,也有所相同,具体如下:
(1)要对ComboBox组件实现数据绑定,首先也是要打开数据表,得到数据集。这和上面TextBox组件的代码大致一样,在此略过。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-46460-7.html
对敢于入侵我领土