下面的代码示例演示如何使用 BindingSource 组件,将三个控件(两个文本框控件和一个 DataGridView 控件)绑定到 DataSet 中的同一列。该示例演示如何处理BindingComplete 事件,并确保当一个文本框的文本值更改时,会用正确的值更新其他文本框和 DataGridView 控件。

该示例使用 BindingSource 来绑定数据源和控件。或者,可以直接将控件绑定到数据源,并从窗体的 BindingContext 检索用于绑定的 BindingManagerBase,然后为BindingManagerBase 处理 BindingComplete 事件。有关如何进行此操作的示例,请参见 BindingManagerBase 的 BindingComplete 事件的相关帮助页。

// Declare the controls to be used.
private BindingSource bindingSource1;
private TextBox textBox1;
private TextBox textBox2;
private DataGridView dataGridView1; private void InitializeControlsAndDataSource()
{
// Initialize the controls and set location, size and
// other basic properties.
this.dataGridView1 = new DataGridView();
this.bindingSource1 = new BindingSource();
this.textBox1 = new TextBox();
this.textBox2 = new TextBox();
this.dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock = DockStyle.Top;
this.dataGridView1.Location = new Point(, );
this.dataGridView1.Size = new Size(, );
this.textBox1.Location = new Point(, );
this.textBox1.Size = new Size(, );
this.textBox2.Location = new Point(, );
this.textBox2.Size = new Size(, );
this.ClientSize = new Size(, );
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGridView1); // Declare the DataSet and add a table and column.
DataSet set1 = new DataSet();
set1.Tables.Add("Menu");
set1.Tables[].Columns.Add("Beverages"); // Add some rows to the table.
set1.Tables[].Rows.Add("coffee");
set1.Tables[].Rows.Add("tea");
set1.Tables[].Rows.Add("hot chocolate");
set1.Tables[].Rows.Add("milk");
set1.Tables[].Rows.Add("orange juice"); // Set the data source to the DataSet.
bindingSource1.DataSource = set1; //Set the DataMember to the Menu table.
bindingSource1.DataMember = "Menu"; // Add the control data bindings.
dataGridView1.DataSource = bindingSource1;
textBox1.DataBindings.Add("Text", bindingSource1,
"Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
textBox2.DataBindings.Add("Text", bindingSource1,
"Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
bindingSource1.BindingComplete +=
new BindingCompleteEventHandler(bindingSource1_BindingComplete);
} private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
{
// Check if the data source has been updated, and that no error has occured.
if (e.BindingCompleteContext ==
BindingCompleteContext.DataSourceUpdate && e.Exception == null) // If not, end the current edit.
e.Binding.BindingManagerBase.EndCurrentEdit();
}

C#:确保绑定到同一数据源的多个控件保持同步的更多相关文章

  1. C# WPF MVVM 实战 – 5- 用绑定,通过 VM 设置 View 的控件焦点

    本文介绍在 MVVM 中,如何用 ViewModel 控制焦点. 这焦点设置个东西嘛,有些争论.就是到底要不要用 ViewModel 来控制视图的键盘输入焦点.这里不讨论,假设你就是要通过 VM,设置 ...

  2. wpf怎么绑定多个值,多个控件

    最近有不少wpf新手问wpf的命令怎么绑定多个控件,很多人为此绞尽脑汁,网上的答案找了也没找到靠谱的,其实用MultiBinding就可以了.从.net 3.0版本开始,就支持MultiBinding ...

  3. ASP.NET控件绑定数据源

    DataList/GridView/Repeater DataSet表示数据集,其中包含表,约束和表之间的关系.与现有数据源的交互通过DataAdapter来控制. 源代码示例: SqlDataAda ...

  4. C#.NET 通用控件数据源绑定类

    using System.Data; using System.Collections; using System.Collections.Generic; using System.Web.UI; ...

  5. ASP.NET中后台数据和前台控件的绑定

    关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...

  6. [置顶] DataGridView控件---绑定数据方法

             DataGridView控件是在windows应用程中显示数据最好的方式,它只需要几行简短的代码就可以把数据显示给用户,同时又支持增.删.改操作.今天将自己总结的增加数据的方法总结分 ...

  7. 在GridControl控件上绑定图片的几种操作方式

    我们知道,基于DevExpress的开发Winform的项目界面的时候,GridControl控件是经常用来绑定数据的,一般以常规的字符内容为主,有时候也会有图片的显示需要,那么如果显示图片,我们应该 ...

  8. 如何: 在 VS中的设计时刻主从表绑定控件到数据库

    这个示例展示了如何在 Visual Studio 2005 的设计时刻,把一个 data-aware 控件 (XtraGrid.XtraPivotGrid.XtraVerticalGrid 等) 绑定 ...

  9. 关于WPF添加右击ContextMeun,以及获取所绑定控件的源

    今天在公司给公司做一个门禁软件,其中有一个添加员工的功能,功能已经做好,但是页面的右边是一个treeView控件,于是我想到再添加员工后,可以在treeview上的部门的TreeViewWithIco ...

随机推荐

  1. 解决小米手机不能运行Android Studio程序的问题

    转载自:解决小米手机不能运行Android Studio程序的问题 问题描述 Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: ...

  2. sql把一个表的某几列的数据存到另一个表里

    一.如何用slq语句把一个表中的某几个字段的数据插入到另一个新表中,就要用下面这条slq语句:    insert into 表名1(字段1,字段2) select 字段1,字段2 from 表名2 ...

  3. 基于nginx实现protobuf RPC

    老婆一起来上海工作,每个月消费立马上来了,做了一个android记账应用,把每笔帐都实时记录进去.开始是单机版的,只能两个人分别记,月底再merge一下.刚好有一台阿里云的ECS,于是准备升级为带服务 ...

  4. [Linux] Systemd 入门教程:实战篇

    reference : http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html 上一篇文章,我介绍了 Systemd ...

  5. Google Ads原理

    Google AdSense广告会根据访问者的地理IP,显示不同的广告,因为adwords里面有相应的设置,有些广告商只想把广告显示给某一地理区域的客户.另一方面就是同一个网站,你在日本和美国会看到日 ...

  6. kafak-python函数使用详解

    Consumer是非线程安全的 Kafka只保证消息不漏,即at lease once,而不保证消息不重.关键点:假如consumer挂了重启,那它将从committed offset位置(告诉ser ...

  7. OpenCV学习(30) 轮廓defects

    上一篇教程中,我们学习了如何计算轮廓的凸包,其实对一个轮廓而言,可能它的凸包和它本身是重合的,也有可能不是重合的.比如下面左边图像的轮廓本身就是凸包,而右边图像的轮廓则不是.我们可以通过函数bool ...

  8. go语言基础之结构体普通变量初始化

    1.结构体 1.1.结构体类型 有时我们需要将不同类型的数据组合成一个有机的整体,如:一个学生有学号/姓名/性别/年龄/地址等属性.显然单独定义以上变量比较繁琐,数据不便于管理. 结构体是一种聚合的数 ...

  9. SQL Server’s Storage Top 10 Best Practices

    好文章, 简明扼要. Storage Top 10 Best Practices http://technet.microsoft.com/en-us/library/cc966534.aspx

  10. iOS开发-Interface Builder的前世今生

    Interface Builder,是用于苹果公司Mac OS X操作系统的软件开发程序,Xcode套件的一部分,于1988年创立.它的创造者Jean-Marie Hullot自称是“一个热爱旅行.充 ...