C#中的数据绑定
来源:互联网 发布:2017公安部网络贩枪案 编辑:程序博客网 时间:2024/04/30 18:39
复杂型组件的数据绑定:
在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的.首先来介绍一下ComboBox组件的数据绑定.
(1).ComboBox组件的数据绑定:
在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:、、.其中是要显示的数据集,是ComboBox组件显示的字段,是实际使用值.具体如下:
ComboBox1.DataSource = myDataSet ;
ComboBox1.DisplayMember = ;
ComboBox1.ValueMember = ;
注释:此时绑定是Access 2000数据库中表的字段.由此可以得到ComboBox组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是Access 2000:
public class Form1 : Form
{
private ComboBox ComboBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ComboBox1.DataSource = myDataSet ;
ComboBox1.DisplayMember = ;
ComboBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
图03:对ComboBox组件数据绑定的程序界面
得到了ComboBox组件对本地数据库的数据绑定程序,也就十分方便的得到ComboBox组件绑定Sql Server 2000源程序代码(Combox02.cs)具体如下:
public class Form1 : Form
{
private ComboBox ComboBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
myConn.Open ( ) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ComboBox1.DataSource = myDataSet ;
ComboBox1.DisplayMember = ;
ComboBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
(2).ListBox组件的数据绑定:
ListBox组件的数据绑定和ComboBox组件的数据绑定的方法大致相同,也是通过设定、.其中这三个属性来完成的.并且这三个属性在ListBox组件中代表的意思和ComboBox组件的意思基本一样.由此可以得到ListBox组件对本地数据库和远程数据库进行数据绑定的源程序.其中ListBox01.cs是对本地数据库进行数据绑定,ListBox02.cs是对远程数据库进行数据绑定,具体如下:
ListBox01.cs源程序代码节??
public class Form1 : Form
{
private ListBox ListBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ListBox1.DataSource = myDataSet ;
ListBox1.DisplayMember = ;
ListBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
图04:对ListBox组件数据绑定的程序界面
以下代码是ListBox组件对Sql Server 2000数据库进行数据绑定的源程序节?↙istBox02.cs):
{
private ListBox ListBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
myConn.Open ( ) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ListBox1.DataSource = myDataSet ;
ListBox1.DisplayMember = ;
ListBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的.首先来介绍一下ComboBox组件的数据绑定.
(1).ComboBox组件的数据绑定:
在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:、、.其中是要显示的数据集,是ComboBox组件显示的字段,是实际使用值.具体如下:
ComboBox1.DataSource = myDataSet ;
ComboBox1.DisplayMember = ;
ComboBox1.ValueMember = ;
注释:此时绑定是Access 2000数据库中表的字段.由此可以得到ComboBox组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是Access 2000:
public class Form1 : Form
{
private ComboBox ComboBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ComboBox1.DataSource = myDataSet ;
ComboBox1.DisplayMember = ;
ComboBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
图03:对ComboBox组件数据绑定的程序界面
得到了ComboBox组件对本地数据库的数据绑定程序,也就十分方便的得到ComboBox组件绑定Sql Server 2000源程序代码(Combox02.cs)具体如下:
public class Form1 : Form
{
private ComboBox ComboBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
myConn.Open ( ) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ComboBox1.DataSource = myDataSet ;
ComboBox1.DisplayMember = ;
ComboBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
(2).ListBox组件的数据绑定:
ListBox组件的数据绑定和ComboBox组件的数据绑定的方法大致相同,也是通过设定、.其中这三个属性来完成的.并且这三个属性在ListBox组件中代表的意思和ComboBox组件的意思基本一样.由此可以得到ListBox组件对本地数据库和远程数据库进行数据绑定的源程序.其中ListBox01.cs是对本地数据库进行数据绑定,ListBox02.cs是对远程数据库进行数据绑定,具体如下:
ListBox01.cs源程序代码节??
public class Form1 : Form
{
private ListBox ListBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ListBox1.DataSource = myDataSet ;
ListBox1.DisplayMember = ;
ListBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
图04:对ListBox组件数据绑定的程序界面
以下代码是ListBox组件对Sql Server 2000数据库进行数据绑定的源程序节?↙istBox02.cs):
{
private ListBox ListBox1 ;
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 = ;
OleDbConnection myConn = new OleDbConnection (strCon) ;
myConn.Open ( ) ;
string strCom = ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
file://把Dataset绑定person数据表
myCommand.Fill (myDataSet , ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
private void button1_Click (object sender , System.EventArgs e)
{
ListBox1.DataSource = myDataSet ;
ListBox1.DisplayMember = ;
ListBox1.ValueMember = ;
}
static void Main ( )
{
Application.Run (new Form1 ( )) ;
}
}
- C#中的数据绑定
- Visual C#中的数据绑定
- 模板中的数据绑定
- Spring中的数据绑定
- Spring中的数据绑定
- C#中的数据绑定
- 数据绑定中的不同
- Flex中的数据绑定
- WPF 中的数据绑定
- AngularJS 中的数据绑定
- Angular2中的数据绑定
- c#Winformdatagridview数据绑定
- Visual C# 数据绑定
- 数据绑定中的数据转换
- ASP.NET中的数据绑定
- asp.net中的数据绑定
- NET中的数据绑定表达式
- .NET中的数据绑定表达式
- 1、重写TextView的onDraw方法
- tar详解及常用命令
- 论.NET反射、委托技术与设计模式关系
- 开平方 如果没有计算器,我们如何求2的平方根? 可以先猜测一个数,比如1.5,然后用2除以这个数字。
- eclipse3.6安装tomcat插件
- C#中的数据绑定
- 单链表应用之多项式加法
- 04.IOS学习笔记之函数
- Linux架构
- ZOJ 3508 The War(贪心)
- 获取服务器上的XML资源,并在客户端上解析出来
- 一个Linux下C线程池的实现
- Linux线程池
- 消失的索引