下面的代码示例演示如何使用 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. 为iOS应用制作一个二维码

    第一步:找到你发布的ios应用的下载链接: 第二步:使用二维码转换器,将下载链接转换成 二维码.http://my.qzone.qq.com/app/100699951.html?via=appcen ...

  2. 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结

    背景 这两天温习了 5 中排序算法,之前也都看过它们的实现,因为没有深入分析的缘故,一直记不住谁是谁,本文就记录一下我学习的一些心得. 三种排序算法可以总结为如下: 都将数组分为已排序部分和未排序部分 ...

  3. Android之RAS加密算法测试

    import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java ...

  4. 一直困扰设计师多年的Android 单位 dp

    轻松理解Android开发单位DP ,让设计与开发高度匹配,整合了网上各大资料汇总一个通俗易懂的. 一直困扰设计师多年的Android 单位 dp 设计这么多年了,相信很多设计师,一直被DP搞得晕头转 ...

  5. Android图片加载框架最全解析(四),玩转Glide的回调与监听

    大家好,今天我们继续学习Glide. 在上一篇文章当中,我带着大家一起深入探究了Glide的缓存机制,我们不光掌握了Glide缓存的使用方法,还通过源码分析对缓存的工作原理进行了了解.虽说上篇文章和本 ...

  6. 在 Sublime Text 2 下开启 Vim 模式

    緣由 由於在 Sublime Text 2 下操作時會想起 Vim 下的鍵盤操作.一時興起在網絡上找了下,發現 Sublime Text 2 是支持類似 Vim 的鍵盤操作的.在此分享下配置過程. 打 ...

  7. 【POJ】【2068】Art Gallery

    计算几何/半平面交 裸的半平面交,关于半平面交的入门请看神犇博客:http://blog.csdn.net/accry/article/details/6070621 然而代码我是抄的proverbs ...

  8. spoj Goblin Wars(简单bfs)

    J - Goblin Wars Time Limit:432MS    Memory Limit:1572864KB    64bit IO Format:%lld & %llu Submit ...

  9. 3维DEMO: 抽奖圆盘

    抽奖圆盘 前些日子去超市,消费满一定钱数可以参加抽奖,就是在电视机上有个可旋转的圆盘,按一键开始,按一键抽奖结束.看到最大奖的扇形区域大约有个10度角的样子,按说中大奖的概率应该是36分之1.当然,这 ...

  10. Informatica 常用组件Source Qualifier之八 Distinct

    如果希望 PowerCenter 从源选择唯一值,您可以使用"选择相异"选项.例如,您可以使用此功能从列出总销售额的表中提取唯一客户标识.使用"选择相异"过滤器 ...