C#应用BindingSource实现数据同步的方法
更新时间:2020年6月25日 11:35 点击:1810
本文以实例形式讲述了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#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25