C#應(yīng)用BindingSource實(shí)現(xiàn)數(shù)據(jù)同步的方法
本文以實(shí)例形式講述了C#應(yīng)用BindingSource實(shí)現(xiàn)數(shù)據(jù)同步的方法,對(duì)C#數(shù)據(jù)庫(kù)程序開(kāi)發(fā)來(lái)說(shuō)具有一定的參考借鑒價(jià)值。具體實(shí)現(xiàn)方法如下:
下面的代碼示例演示如何使用 BindingSource 組件,將三個(gè)控件(兩個(gè)文本框控件和一個(gè) DataGridView 控件)綁定到 DataSet 中的同一列。
該示例演示如何處理 BindingComplete 事件,并確保當(dāng)一個(gè)文本框的文本值更改時(shí),會(huì)用正確的值更新其他文本框和 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 組件跨窗體共享綁定數(shù)據(jù),具體代碼如下:
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 });
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C# 委托的三種調(diào)用示例(同步調(diào)用 異步調(diào)用 異步回調(diào))
- c#(Socket)同步套接字代碼示例
- c#.net多線程編程教學(xué)——線程同步
- C#同步、異步遠(yuǎn)程下載文件實(shí)例
- c#實(shí)現(xiàn)數(shù)據(jù)同步的方法(使用文件監(jiān)控對(duì)象filesystemwatcher)
- C#同步網(wǎng)絡(luò)時(shí)間的方法實(shí)例詳解
- c#線程同步使用詳解示例
- 解析C#中委托的同步調(diào)用與異步調(diào)用(實(shí)例詳解)
- 基于C#實(shí)現(xiàn)的多生產(chǎn)者多消費(fèi)者同步問(wèn)題實(shí)例
- C#使用AutoResetEvent實(shí)現(xiàn)同步
相關(guān)文章
c#創(chuàng)建windows服務(wù)入門(mén)教程實(shí)例
windows服務(wù)是windows系統(tǒng)中一類(lèi)特殊的應(yīng)用程序,一般情況下它們只會(huì)在后臺(tái)運(yùn)行,不會(huì)影響前臺(tái)操作,非常適合做一些不需要用戶參與的而又需要長(zhǎng)時(shí)間執(zhí)行的任務(wù)2014-04-04
深入U(xiǎn)nix時(shí)間戳與C# DateTime時(shí)間類(lèi)型互換的詳解
本篇文章是對(duì)Unix時(shí)間戳與C# DateTime時(shí)間類(lèi)型互換進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
C#根據(jù)前臺(tái)傳入實(shí)體名稱實(shí)現(xiàn)動(dòng)態(tài)查詢數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#如何根據(jù)前臺(tái)傳入實(shí)體名稱實(shí)現(xiàn)動(dòng)態(tài)查詢數(shù)據(jù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04
使用位運(yùn)算實(shí)現(xiàn)網(wǎng)頁(yè)中的過(guò)濾、篩選功能實(shí)例
這篇文章主要介紹了使用位運(yùn)算實(shí)現(xiàn)網(wǎng)頁(yè)中的過(guò)濾、篩選功能實(shí)例,一個(gè)比常規(guī)拼接SQL字符串更有新意的一個(gè)解決思路,需要的朋友可以參考下2014-07-07
C#編寫(xiě)的windows計(jì)算器的實(shí)例代碼
這篇文章介紹了C#編寫(xiě)windows計(jì)算器的代碼,有需要的朋友可以參考一下2013-07-07
C#使用RegNotifyChangeKeyValue監(jiān)聽(tīng)注冊(cè)表更改的方法小結(jié)
RegNotifyChangeKeyValue的最后一個(gè)參數(shù)傳遞false,表示以同步的方式監(jiān)聽(tīng),這篇文章主要介紹了C#使用RegNotifyChangeKeyValue監(jiān)聽(tīng)注冊(cè)表更改的方法小結(jié),需要的朋友可以參考下2024-06-06

