第8章使用C#开发Windows数据库应用程序
时间:2026-01-20
时间:2026-01-20
数据库基本知识~
第8章使用C#开发Windows数据库应用程序本章要点 简单数据绑订 复杂数据绑订 数据源的类型 DataGrid的使用 Windows应用程序开发实例
数据库原理及应用
2013年7月9日星期二
数据库基本知识~
8.1 数据绑定数据绑定是指数据源元素与图形界面的接口技术,如使 用TextBox控件绑定到表中的单个值。窗体控件可以按照 两种 方式绑定数据:简单数据绑定和复杂数据绑定。 Windows数据绑定控件(如Label、Button或TextBox能够绑 定数据,因为Binding类提供了这样的功能 (System.Windows.Forms.Binding),这个类由.NET Framework 提供,负责在每个控件属性和数据源中的数据元素之间创建简 单的绑定。如:textBox1.DataBindings.Add(″Text″,da,″Employees.FirstName″);
控件的DataBindings属性可以使用Add方法添加其中的每个 属性。Add方法有三个参数。第一个参数是控件属性的名称, 如TextBox控件的Text属性或者DataGrid控件的DataSource属性。 第二个参数是下列任意类,或者实现下列任意接口的类的实例, 如表8-1列出几个接口及实现这些接口的类。数据库原理及应用2013年7月9日星期二
数据库基本知识~
第三个参数描述数据源中的数据成员。它是必须能转 化为标量值的字符串文字,如使用DataSet时要根据表名称 所选的列。 表8-1 可用于Binding类构造函数的第二个参数的类接口 ICollection 实现接口的一些类 Array,BitArray
IListSourceITypedList
DataSet,DataTableDataView,DataViewManager
如果想显式声明Binding对象,则可以使用类似下面的 代码,例如,想把TextBox控件的Text属性绑定到表中的数 据元素上。Binding newBind=new Binding(″Text″,ds,″Employees.FirstName″); textBox1.DataBindings.Add(newBind);
数据库原理及应用
2013年7月9日星期二
数据库基本知识~
8.1.1 简单数据绑定简单数据绑定是指每个控件属性与数据源的单一元素之 间的一对一关系。下面的例子演示简单数据绑定,将一组 TextBox 控件上的 Text 属性绑定到作为客户列表存储的 Customer 对象的属性。在控件上使用 DataBindings 集合添加 简单数据绑定。textBoxID.DataBindings.Add("Text", custList, "CustomerID"); textBoxTitle.DataBindings.Add("Text", custList, "ContactTitle"); textBoxLastName.DataBindings.Add("Text", custList, "ContactName"); textBoxFirstName.DataBindings.Add("Text", custList, "CompanyName"); textBoxAddress.DataBindings.Add("Text", custList, "Address");
每个TextBox.Text都绑定到当前 Customer 对象,如同 BindingContext 进行维护一样。若要更改当前对象,可使用 BindingContext 递增或递减集合的 Position 属性。数据库原理及应用
2013年7月9日星期二
数据库基本知识~
例如,通过按如下所示处理按钮的Click事件实现 Move Next 按钮。private void buttonMoveNext_Click(object sender, System.EventArgs e) { this.BindingContext[custList].
Position++; }
每当位置更改时,BindingContext 引发一个事件。this.BindingContext[custList].PositionChanged += new System.EventHandler(customer_PositionChanged); private void customer_PositionChanged(object sender, System.EventArgs e) { textBoxPosition.Text = "Record " + (this.BindingContext[custList].Position } + 1)+ " of " + custList.Length;
数据库原理及应用
2013年7月9日星期二
数据库基本知识~
8.1.2 复杂数据绑定复杂数据绑定指将控件绑定到集合(而不是将控件绑定 到集合内的单个项)。下面的代码将 ComboBox 绑定到 State 对象的一个数组。public struct State { private string shortName, longName; public State(string longName , string shortName) { this.shortName = shortName ; this.longName = longName ; } public string ShortName { get { return shortName; } } public string LongName { get { return longName; } } } private State[] States = new State[] {new State("Alabama","AL"), new State("Washington" ,"WA),} comboBoxState.DataSource=States; comboBoxState.DisplayMember="LongName";
数据库原理及应用
2013年7月9日星期二
数据库基本知识~
8.2 数据源的类型8.2.1 数组作为数据源在大多数情况下,Array最适合于存储和检索一致的数据。 数组在运行时支持对数据的处理,且容易在代码中通过 ICollection接口使用。 例如,private void Form1_Load(object sender, System.EventArgs e) { String[] book = new String[] {"操作系统","2005年4月","25.00元"}; textBox1.DataBindings.Add("Text",book,null); }
例如编写如下代码可以实现数组元素遍历的功能:BindingManagerBase bManager=this.BindingContext[book,null]; bManager.Position += 1;
数据库原理及应用
2013年7月9日星期二
数据库基本知识~
8.2.2 数据表作为数据源DataTable类实现IListSource接口。DataTable数据源既可 用于简单绑定例子,也可用于复杂绑定例子。把DataTable绑 定到控件可以有两种方式,一种是把整个表绑定到支持复杂 绑定的控件上(可以一次显示多个记录的控件),另一种是 把单个的列绑定到支持简单绑定的控件上。 下面这个例子把ListBox控件绑定到了从填充过的DataSet 提取的DataTable对象上,如下面的代码所示:DataTable myTable=ds.Tables[″Employees″]; listBox1.DataSource=myTable; listBox1.DisplayMember=″FirstName″;
下面的代码通过给DataBindings集合添加绑定以及将 Da …… 此处隐藏:3459字,全部文档内容请下载后查看。喜欢就下载吧 ……