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 ...
随机推荐
- 通过微软的HTML Help Workshop 利用.html文件 生成简单的chm帮助类的文件
1.下载并安装Microsoft HTML Help Workshop 下载链接:http://www.microsoft.com/en-us/download/details.aspx?id=211 ...
- python笔记28-lxml.etree爬取html内容
前言 本篇继续lxml.etree学习,在线访问接口,通过接口返回的html,解析出想要的text文本内容 环境准备: python 3.6 lxml requets 定位目标 爬取我的博客首页htt ...
- Java 中的 int 与 Integer 用于 List<Integer> 时,以及通过打印变量检測程序执行和函数调用次数计数
总结一下近期做的东西中遇到的问题 1. Java 中的 int 与 Integer 用于 List<Integer> 时 两者之间的关系都是非常清楚的.int 是基本数据类型,存储的是值 ...
- Java 8 的新特性和改进总览
这篇文章是对Java 8中即将到来的改进做一个面向开发者的综合性的总结,JDK的这一特性将会在2013年9月份发布. 在写这篇文章的时候,Java 8的开发工作仍然在紧张有序的进行中,语言特新和API ...
- maven+jenkins+jmeter性能测试:maven把项目依赖拷贝到项目指定位置
先上pom.xml配置: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...
- 【BestCoder】【Round#41】
枚举+组合数?+DP+数学问题 http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=582 QAQ许久没打过比赛,来一发BC,结果还是只 ...
- 在Spark上运行TopK程序
1. scala程序如下 package com.cn.gao import org.apache.spark.SparkConf import org.apache.spark.SparkConte ...
- 机器学习理论与实战(十一)关联规则分析Apriori
<机器学习实战>的最后的两个算法对我来说有点陌生,但学过后感觉蛮好玩,了解了一般的商品数据关联分析和搜索引擎智能提示的工作原理.先来看看关联分析(association analysis) ...
- 添加sqljdbc4的maven依赖
sqljdbc是微软sql server的jdbc驱动 使用sqljdbc需要从微软的官方网站下载jar包: http://www.microsoft.com/en-us/download/detai ...
- Transformer中引用iqd作为数据源的时提示TR1008无法连接问题
刚接触tr的同行们可能都会遇到这么一个问题,当然我也遇到了. 今天把问题的解决办法详细的说明一下 从问题的表面来看是从cognos.ini文件缺少了什么配置东西,但是cognos10的安装中没有cog ...