文档详情

第八章使用C#开发Windows数据库应用程序.ppt

发布:2017-04-25约5.01千字共12页下载文档
文本预览下载声明
第8章使用C#开发Windows数据库应用程序 ;8.1 数据绑定; 第三个参数描述数据源中的数据成员。它是必须能转化为标量值的字符串文字,如使用DataSet时要根据表名称所选的列。 表8-1 可用于Binding类构造函数的第二个参数的类;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 属性。 ; 例如,通过按如下所示处理按钮的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; } ;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;;8.2 数据源的类型 8.2.1 数组作为数据源 在大
显示全部
相似文档