C#应用BindingSource实现数据同步的方法
作者:shichen2014 发布时间:2021-07-09 16:15:48
标签:C#,BindingSource
本文以实例形式讲述了C#应用BindingSource实现数据同步的方法,对C#数据库程序开发来说具有一定的参考借鉴价值。具体实现方法如下:
下面的代码示例演示如何使用 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 组件跨窗体共享绑定数据,具体代码如下:
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&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 });
}
}
}
希望本文所述对大家的C#程序设计有所帮助。


猜你喜欢
- 本文我想跟大家分享的是如何将 C# 中的一些图像对象保存到 Oracle 中的 BLOB 字段中,这里我们并不想从零开始,而是使用我自己的框
- javax.el.ELException的解决方式failed to parse the expression [${xxx}]Tomcat
- spring容器初始化Bean操作在某些情况下,Spring容器在初始化Bean的时候,希望在初始化bean前和销毁bean前进行一些资源的
- volatile 变量提供了线程的可见性,并不能保证线程安全性和原子性。什么是线程的可见性:锁提供了两种主要特性:互斥(mutual exc
- 引言这里实现一个简单的图片上传功能,主要是熟悉这个文件上传的交互流程。关于更复杂的文件上传,如大文件的切片上传、断点续传等,这里不做过多介绍
- @GetMapping注解携带参数方式今天突然发现,当我们根据id查询用户信息时,如果不想通过localhost:8080//findOne
- 这几天面试中有遇到关于main数组中的args数组传值的问题,一般是从命令提示符中传值,也可以直接在java代码中赋值。而且这个数组的长度是
- MyBatis查询数据赋值给List集合数据缺少今天在使用MyBatis查询数据时,发现查出来的数据和List集合的大小不一致,如下图所示,
- 在Winform开发中中,我们为了方便客户选择,往往使用系统的字典数据选择,毕竟选择总比输入来的快捷、统一,一般我们都会简单封装一下,以便方
- 内部类基本介绍一个类的内部又完整的嵌套了另一个类结构。被嵌套的类称为内部类(inner class),嵌套其他类的类称为外部类(outer
- 接口直接返回图片数据起因最近在做涉及到分享推广的业务,需要由业务员分享二维码进入推广页面,由于是新项目,前期预算和用量都有限,没有搭建对象存
- Convert.ToInt32、int.Parse(Int32.Parse)、int.TryParse、(int) 四者都可以解释为将类型转
- 对接支付宝支付接口,官方文档已经写的很清楚了,但是也有很多像我一样的小白,第一次对接支付宝支付接口,会有些迷茫,所以我在此写下这篇文章,给我
- 前言在前一节的学习中,慕歌带大家使用了全局结果集返回,通过使用全局结果集配置,优雅的返回后端数据,为前端的数据拿取提供了非常好的参考。同时通
- 方法一,修改gradle.properties文件,增加一句gradle.user.home=D\:\\Android\\.gradle但这
- @Autowired加到接口上但获取的是实现类问题Spring的@Autowired加到接口上但获取的是实现类? &
- 代码注释是架起程序设计者与程序阅读者之间的通信桥梁,最大限度的提高团队开发合作效率。也是程序代码可维护性的重要环节之一。所以我们不是为写注释
- 本文实例为大家分享了C语言实现简单弹跳小球的具体代码,供大家参考,具体内容如下本节利用 printf 函数 实现一个在屏幕上弹跳的小球,内容
- 一:在函数入参中使用通配符@AspectJ支持3种通配符* :匹配任意字符,但它只能匹配上下文中的一个元素... :匹配任意字符,可以匹配上
- 在学习操作系统这本书的时候,我们使用的是汤小丹老师的《计算机操作系统》接下来我将会使用java语言去实现内部代码。Swap指令最佳置换算法是