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

数据同步


// 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(0, 0);
this.dataGridView1.Size = new Size(292, 150);
this.textBox1.Location = new Point(132, 156);
this.textBox1.Size = new Size(100, 20);
this.textBox2.Location = new Point(12, 156);
this.textBox2.Size = new Size(100, 20);
this.ClientSize = new Size(292, 266);
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[0].Columns.Add("Beverages");

// Add some rows to the table.
set1.Tables[0].Rows.Add("coffee");
set1.Tables[0].Rows.Add("tea");
set1.Tables[0].Rows.Add("hot chocolate");
set1.Tables[0].Rows.Add("milk");
set1.Tables[0].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();
}

//************************************************
如何:使用 BindingSource 组件跨窗体共享绑定数据
//************************************************

使用 BindingSource 组件跨窗体共享绑定数据


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace BindingSourceMultipleForms
{
public class MainForm : Form
{
public MainForm()
{
this.Load += new EventHandler(MainForm_Load);
}

private BindingSource bindingSource1;
private Button button1;

private void MainForm_Load(object sender, EventArgs e)
{
InitializeData();
}

private void InitializeData()
{
bindingSource1 = new System.Windows.Forms.BindingSource();

// Handle the BindingComplete event to ensure the two forms
// remain synchronized.
bindingSource1.BindingComplete +=
new BindingCompleteEventHandler(bindingSource1_BindingComplete);
ClientSize = new System.Drawing.Size(292, 266);
DataSet dataset1 = new DataSet();

// Some xml data to populate the DataSet with.
string musicXml =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<music>" +
"<recording><artist>Dave Matthews</artist>" +
"<cd>Under the Table and Dreaming</cd>" +
"<releaseDate>1994</releaseDate><rating>3.5</rating></recording>" +
"<recording><artist>Coldplay</artist><cd>X&amp;Y</cd>" +
"<releaseDate>2005</releaseDate><rating>4</rating></recording>" +
"<recording><artist>Dave Matthews</artist>" +
"<cd>Live at Red Rocks</cd>" +
"<releaseDate>1997</releaseDate><rating>4</rating></recording>" +
"<recording><artist>U2</artist>" +
"<cd>Joshua Tree</cd><releaseDate>1987</releaseDate>" +
"<rating>5</rating></recording>" +
"<recording><artist>U2</artist>" +
"<cd>How to Dismantle an Atomic Bomb</cd>" +
"<releaseDate>2004</releaseDate><rating>4.5</rating></recording>" +
"<recording><artist>Natalie Merchant</artist>" +
"<cd>Tigerlily</cd><releaseDate>1995</releaseDate>" +
"<rating>3.5</rating></recording>" +
"</music>";

// Read the xml.
System.IO.StringReader reader = new System.IO.StringReader(musicXml);
dataset1.ReadXml(reader);

// Get a DataView of the table contained in the dataset.
DataTableCollection tables = dataset1.Tables;
DataView view1 = new DataView(tables[0]);

// Create a DataGridView control and add it to the form.
DataGridView datagridview1 = new DataGridView();
datagridview1.ReadOnly = true;
datagridview1.AutoGenerateColumns = true;
datagridview1.Width = 300;
this.Controls.Add(datagridview1);
bindingSource1.DataSource = view1;
datagridview1.DataSource = bindingSource1;
datagridview1.Columns.Remove("artist");
datagridview1.Columns.Remove("releaseDate");

// Create and add a button to the form.
button1 = new Button();
button1.AutoSize = true;
button1.Text = "Show/Edit Details";
this.Controls.Add(button1);
button1.Location = new Point(50, 200);
button1.Click += new EventHandler(button1_Click);
}

// Handle the BindingComplete event to ensure the two forms
// remain synchronized.
private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
{
if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate
&& e.Exception == null)
e.Binding.BindingManagerBase.EndCurrentEdit();
}

// The detailed form will be shown when the button is clicked.
private void button1_Click(object sender, EventArgs e)
{
DetailForm detailForm = new DetailForm(bindingSource1);
detailForm.Show();
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}

// The detail form class.
public class DetailForm : Form
{
private BindingSource formDataSource;

// The constructor takes a BindingSource object.
public DetailForm(BindingSource dataSource)
{
formDataSource = dataSource;
this.ClientSize = new Size(240, 200);
TextBox textBox1 = new TextBox();
this.Text = "Selection Details";
textBox1.Width = 220;
TextBox textBox2 = new TextBox();
TextBox textBox3 = new TextBox();
TextBox textBox4 = new TextBox();
textBox4.Width = 30;
textBox3.Width = 50;

// Associate each text box with a column from the data source.
textBox1.DataBindings.Add("Text", formDataSource, "cd", true, DataSourceUpdateMode.OnPropertyChanged);

textBox2.DataBindings.Add("Text", formDataSource, "artist", true);
textBox3.DataBindings.Add("Text", formDataSource, "releaseDate", true);
textBox4.DataBindings.Add("Text", formDataSource, "rating", true);
textBox1.Location = new Point(10, 10);
textBox2.Location = new Point(10, 40);
textBox3.Location = new Point(10, 80);
textBox4.Location = new Point(10, 120);
this.Controls.AddRange(new Control[] { textBox1, textBox2, textBox3, textBox4 });
}

}
}

转载于:https://www.cnblogs.com/08shiyan/archive/2010/08/13/1798652.html

《(学习笔记)两天进步一点点》(3)——应用BindingSource实现数据同步相关推荐

  1. cockroachdb mysql_CockroachDB学习笔记——[译]CockroachDB中的SQL:映射表中数据到键值存储...

    CockroachDB学习笔记--[译]CockroachDB中的SQL:映射表中数据到键值存储 原文标题:SQL in CockroachDB: Mapping Table Data to Key- ...

  2. Kinect开发学习笔记之(六)带游戏者ID的深度数据的提取

    Kinect开发学习笔记之(六)带游戏者ID的深度数据的提取 zouxy09@qq.com http://blog.csdn.net/zouxy09 我的Kinect开发平台是: Win7x86 + ...

  3. 【学习笔记】C++语言程序设计(郑莉):数据的共享与保护

    [学习笔记]C++语言程序设计(郑莉):数据的共享与保护 1. 标识符的作用域与可见性 1.1 作用域 1.1.1 函数原型作用域 1.1.2 局部作用域 1.1.3 类作用域 1.1.4 命名空间作 ...

  4. MongoDB学习笔记(四)使用Java进行实时监控与数据收集(空间使用量、连接数)

    目录: MongoDB学习笔记(一)环境搭建与常用操作 MongoDB学习笔记(二)使用Java操作MongoDB MongoDB学习笔记(三)使用Spring Data操作MongoDB Mongo ...

  5. oracle如何往dg加盘_学习笔记:Oracle DG系统 主备库中表空间和数据文件增加删除等管...

    天萃荷净 Oracle Data Guard表空间和数据文件管理汇总 汇总日常工作中操作,在Oracle DG结构系统中,如何删除备库表空间和数据文件,如何管理主库与备库之间的文件系统,详见文章内容. ...

  6. 安卓学习笔记07:事件处理、窗口跳转与传递数据

    文章目录 零.学习目标 一.三个基本控件 1.标签控件(TextView) 2.编辑框控件(EditText) 3.按钮控件(Button) 二.安卓事件处理机制 (一)安卓事件处理概述 (二)安卓事 ...

  7. 【Python学习笔记—保姆版】第四章—关于Pandas、数据准备、数据处理、数据分析、数据可视化

    第四章 欢迎访问我搞事情的[知乎账号]:Coffee 以及我的[B站漫威剪辑账号]:VideosMan 若我的笔记对你有帮助,请用小小的手指,点一个大大的赞哦. #编译器使用的是sypder,其中&q ...

  8. 【数据库和SQL学习笔记】1.SQL语言的功能和特点,数据定义语言和应用,主键和外键

    本专栏是我对数据库系统和SQL语言的学习笔记分享~ 数据库系统软件:SQL Server 2019 Express(免费,初学者使用足够,足够部署小型项目) 操作系统:Windows 10 安装过程略 ...

  9. 数据治理专业认证CDMP学习笔记(思维导图与知识点)- 第八章数据集成和互操作篇...

    大家好,我是独孤风,一位曾经的港口煤炭工人,目前在某国企任大数据负责人,公众号大数据流动主理人.在最近的两年的时间里,因为公司的需求,还有大数据的发展趋势所在,我开始学习数据治理的相关知识. 数据治理 ...

  10. 学习笔记-------两阶段提交 2PC

最新文章

  1. linux 文件属性文件权限
  2. Dubbo架构设计详解
  3. 华为RH2288V3服务器部署指南
  4. 漫游Kafka设计篇之Producer和Consumer
  5. mysql 创建表 引号_Mysql建表语句中显示双引号的方法介绍
  6. 利用UTL_FILE包实现文件I/O操作
  7. 谈谈python enumerate()函数的用法_python enumerate函数的使用方法总结
  8. 学习HttpClient,从两个小例子开始
  9. iOS底层探索之Runtime(二): objc_msgSend汇编快速查找分析
  10. IR2104驱动原理--恩智浦智能车电机驱动
  11. 无法定位程序输入点dxgiget_无法定位程序输入点于动态链接库dxgi.dll上怎么解决?-51dll帮助...
  12. ACL 2022 | PLATO-LTM:具有长期记忆机制的对话生成框架
  13. Android自定义导览地图组件(二)
  14. 加油站都需要什么手续_开办加油站需要办哪些手续?
  15. APP商店货币化的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  16. 阿里云的免费云虚拟主机
  17. 使用RTOS系统如何选取大容量存储芯片NAND FLASH
  18. 高等数学——傅里叶级数
  19. RS推荐系统-关联规则-Apriori
  20. 【日常填坑】启动WebLogic时提示:此时不应有XXXX

热门文章

  1. HTML 5 span 标签
  2. 为什么「margin:auto」可以让块级元素水平居中?
  3. [label][paypal] Paypal 支付页面的语言显示问题
  4. iOS.UIKit.07.UIAlertView_UIActionSheet
  5. hdu1865 1sting
  6. openapi and light-4j
  7. php安装oci8和pdo_oci扩展实现连接oracle数据库
  8. docker+elasticsearch的安装
  9. 补坑:Prufer 编码总结
  10. 使用 classList API