2024年5月2日发(作者:)

Visual Studio C#中的数据绑定

五. 复杂型组件的数据绑定:

在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的。首

先来介绍一下ComboBox组件的数据绑定.

(1).ComboBox组件的数据绑定:

在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:、

"DisplayMember"、"ValueMember"。其中"DataSource"是要显示的数据集,"DisplayMember"是ComboBox

组件显示的字段,"ValueMember"是实际使用值。具体如下:

urce = myDataSet ;

yMember = "" ;

ember = "" ;

注释:此时绑定是Access 2000数据库中"person"表的"xm"字段。由此可以得到ComboBox组件数据

绑定的源程序代码(),本代码操作数据库是Access 2000:

public class Form1 : Form

{

private ComboBox ComboBox1 ;

private Button button1 ;

private t myDataSet ;

private ner components = null ;

public Form1 ( )

{

file://打开数据链接,得到数据集

GetConnect ( ) ;

InitializeComponent ( ) ;

}

file://清除程序中使用过的资源

protected override void Dispose ( bool disposing )

{

if ( disposing )

{

if ( components != null )

{

e ( ) ;

}

}

e ( disposing ) ;

}

private void GetConnect ( )

{

file://创建一个 OleDbConnection

string strCon = " Provider = .4.0 ; Data Source = " ;

OleDbConnection myConn = new OleDbConnection ( strCon ) ;

string strCom = " SELECT * FROM person " ;

file://创建一个 DataSet

myDataSet = new DataSet ( ) ;

( ) ;

file://用 OleDbDataAdapter 得到一个数据集

OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

file://把Dataset绑定person数据表

( myDataSet , "person" ) ;

file://关闭此OleDbConnection

( ) ;

}

private void button1_Click ( object sender , rgs e )

{

urce = myDataSet ;

yMember = "" ;

ember = "" ;

}

static void Main ( )

{

( new Form1 ( ) ) ;

}

}

图03:对ComboBox组件数据绑定的程序界面

得到了ComboBox组件对本地数据库的数据绑定程序,也就十分方便的得到ComboBox组件绑定Sql

Server 2000源程序代码()具体如下: