数据绑定代码如下:
dateTimePickerDocOn.DataBindings.Add(new System.Windows.Forms.Binding("Value" , doc , "Summary.DocNo"));
numericUpDownBatch.DataBindings.Add(new System.Windows.Forms.Binding("Value" , doc , "Summary.Batch"));
numericUpDownBoxes.DataBindings.Add(new System.Windows.Forms.Binding("Value" , doc , "Summary.Boxes"));
textBoxCharge.DataBindings.Add(new System.Windows.Forms.Binding("Text" , doc , "Summary.Charge));
textBoxDocNo.DataBindings.Add(new System.Windows.Forms.Binding("Text" , doc ,"Summary.DocNo"));
textBoxNotes.DataBindings.Add(new System.Windows.Forms.Binding("Text" , doc , "Summary.Notes"));
dataGridItems.SetDataBinding(doc , "Items");
bmSummary = this.BindingContext[this.doc , "Summary"];
bmItems = this.BindingContext[this.doc , "Items"];
只有DataGrid所绑定的数据可显示,其余绑定都不显示
dateTimePickerDocOn.DataBindings.Add(new System.Windows.Forms.Binding("Value" , doc , "Summary.DocNo"));
numericUpDownBatch.DataBindings.Add(new System.Windows.Forms.Binding("Value" , doc , "Summary.Batch"));
numericUpDownBoxes.DataBindings.Add(new System.Windows.Forms.Binding("Value" , doc , "Summary.Boxes"));
textBoxCharge.DataBindings.Add(new System.Windows.Forms.Binding("Text" , doc , "Summary.Charge));
textBoxDocNo.DataBindings.Add(new System.Windows.Forms.Binding("Text" , doc ,"Summary.DocNo"));
textBoxNotes.DataBindings.Add(new System.Windows.Forms.Binding("Text" , doc , "Summary.Notes"));
dataGridItems.SetDataBinding(doc , "Items");
bmSummary = this.BindingContext[this.doc , "Summary"];
bmItems = this.BindingContext[this.doc , "Items"];
只有DataGrid所绑定的数据可显示,其余绑定都不显示
id 数字 序号
xm 文本 姓名
xb 文本 性别
nl 文本 年龄
zip 文本 邮政编码 (2).远程数据库Sql Server 2000的数据库服务器名称为"Server1",数据库名称为"Data1",登陆的ID为"sa",口令为空,在数据库也定义了一张"person"表,数据结构如上表。
label1.DataBindings.Add ( "Text" , myDataSet , "person.xm" ) ; 注释:此时绑定是Access 2000数据库中"person"表的"xm"字段。由此可以得到Label组件数据绑定的源程序代码(Label01.cs),本代码操作数据库是Access 2000:
public class Form1 : Form
{
private Label label1 ;
private Button button1 ;
private System.Data.DataSet myDataSet ;
private System.ComponentModel.Container components = null ;public Form1 ( )
{
file://打开数据链接,得到数据集
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}private void GetConnect ( )
{
file://创建一个 OleDbConnection
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM person " ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://把Dataset绑定person数据表
myCommand.Fill ( myDataSet , "person" ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;}private void button1_Click ( object sender , System.EventArgs e )
{
label1.DataBindings.Add ( "Text" , myDataSet , "person.xm" ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
}
图02:对Label组件数据绑定后的程序界面 得到了Label组件对Access 2000数据库数据绑定的程序代码,改换一下程序中数据链接,就可以得到Label组件对Sql Server 2000数据库数据绑定的源程序代码(Label02.cs),具体如下:public class Form1 : Form
{
private Label label1 ;
private Button button1 ;
private System.Data.DataSet myDataSet ;
private System.ComponentModel.Container components = null ;public Form1 ( )
{
file://打开数据链接,得到数据集
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}private void GetConnect ( )
{
// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1
string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
string strCom = " SELECT * FROM person " ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://把Dataset绑定person数据表
myCommand.Fill ( myDataSet , " person " ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}private void button1_Click ( object sender , System.EventArgs e )
{
label1.DataBindings.Add ( "Text" , myDataSet , "person.xm" ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
}
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM person " ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://把Dataset绑定person数据表
myCommand.Fill ( myDataSet , "person" ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
(2). 连接Sql Server 2000,得到DataSet:// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1
string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
string strCom = " SELECT * FROM person " ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://把Dataset绑定person数据表
myCommand.Fill ( myDataSet , " person " ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ; (二).根据不同组件,采用不同的数据绑定: 对于简单型的数据绑定,数据绑定的方法其实比较简单,在得到数据集以后,一般是通过把数据集中的某个字段绑定到组件的显示属性上面,譬如TextBox组件和Label组件,是绑定到"Text"属性。对于复杂型的数据绑定一般是通过设定其某些属性值来实现绑定的。这些下面将会具体介绍。
1.取消先前绑定
2.得到DataSet或填充DataSet,
3.绑定数据
感谢各位的帮助!