C#:确保绑定到同一数据源的多个控件保持同步
下面的代码示例演示如何使用 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#:确保绑定到同一数据源的多个控件保持同步的更多相关文章
- C# WPF MVVM 实战 – 5- 用绑定,通过 VM 设置 View 的控件焦点
本文介绍在 MVVM 中,如何用 ViewModel 控制焦点. 这焦点设置个东西嘛,有些争论.就是到底要不要用 ViewModel 来控制视图的键盘输入焦点.这里不讨论,假设你就是要通过 VM,设置 ...
- wpf怎么绑定多个值,多个控件
最近有不少wpf新手问wpf的命令怎么绑定多个控件,很多人为此绞尽脑汁,网上的答案找了也没找到靠谱的,其实用MultiBinding就可以了.从.net 3.0版本开始,就支持MultiBinding ...
- ASP.NET控件绑定数据源
DataList/GridView/Repeater DataSet表示数据集,其中包含表,约束和表之间的关系.与现有数据源的交互通过DataAdapter来控制. 源代码示例: SqlDataAda ...
- C#.NET 通用控件数据源绑定类
using System.Data; using System.Collections; using System.Collections.Generic; using System.Web.UI; ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- [置顶] DataGridView控件---绑定数据方法
DataGridView控件是在windows应用程中显示数据最好的方式,它只需要几行简短的代码就可以把数据显示给用户,同时又支持增.删.改操作.今天将自己总结的增加数据的方法总结分 ...
- 在GridControl控件上绑定图片的几种操作方式
我们知道,基于DevExpress的开发Winform的项目界面的时候,GridControl控件是经常用来绑定数据的,一般以常规的字符内容为主,有时候也会有图片的显示需要,那么如果显示图片,我们应该 ...
- 如何: 在 VS中的设计时刻主从表绑定控件到数据库
这个示例展示了如何在 Visual Studio 2005 的设计时刻,把一个 data-aware 控件 (XtraGrid.XtraPivotGrid.XtraVerticalGrid 等) 绑定 ...
- 关于WPF添加右击ContextMeun,以及获取所绑定控件的源
今天在公司给公司做一个门禁软件,其中有一个添加员工的功能,功能已经做好,但是页面的右边是一个treeView控件,于是我想到再添加员工后,可以在treeview上的部门的TreeViewWithIco ...
随机推荐
- UI---------EventSystem
创建uGUI控件后,Unity会同时创建一个叫EventSystem的GameObject,用于控制各类事件.可以看到Unity自带了两个Input Module,一个用于响应标准输入,一个用于响应触 ...
- window.open("url?param="+paramvalue) 服务端 乱码问题解决
window.open("url?param="+paramvalue)传递参数出现乱码,在客房端显示是正常的,可是到服务端就是乱码. 1. 利用一个js在客户端转码的函数,esc ...
- Shimmer辉光动画效果
Shimmer辉光动画效果 效果 源码 https://github.com/facebook/Shimmer https://github.com/YouXianMing/Animations // ...
- 很酷的C语言技巧
C语言常常让人觉得它所能表达的东西非常有限.它不具有类似第一级函数和模式匹配这样的高级功能.但是C非常简单,并且仍然有一些非常有用的语法技巧和功能,只是没有多少人知道罢了. 指定的初始化 很多人都知道 ...
- 从win10家庭版/中文版升级到win10专业版
发布时间:2015-8-4 很多同学在不了解win10系统版本的情况下,安装了win10家庭版/中文版,不管是win10家庭版,还是win10家庭中文版,或者是win10家庭单语言版,它们都是 ...
- Git 学习(三)本地仓库操作——git add & commit
Git 学习(三)本地仓库操作——git add & commit Git 和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念.这在上文已有提及,本文具体说明什么是工作区及暂存区,以及 ...
- JavaBean的应用
1. 获取JavaBean属性信息 例1.1 在JSP页面中显示JavaBean属性信息. (1)创建名称为Produce的类,该类是封装商品对象的JavaBean,在Produce类中创建商品属性, ...
- Java实现对Mysql的图片存取操作
1.MySQL中的BLOB类型 Mysql中可以存储大文件数据,一般使用的BLOB对象.如图片,视频等等. BLOB是一个二进制大对象,可以容纳可变数量的数据.因为是二进制对象,所以与编码方式无关.有 ...
- Linux C Socket编程发送结构体、文件详解及实例
利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...
- Android下的Junit测试
Android SDK 1.5已经将JUnit包含进来了,用过一次,昨天晚上重新用的时候还出了一点问题,还是决定写一篇比较详细的文章,供大家和自己以后使用,写起来也挺方便的,Android下的Juni ...