This paper describes the method of C # application BindingSource to realize data synchronization by example, which has some reference value for the development of C # database program. The implementation method is as follows:
The following code example demonstrates how to use the BindingSource component to bind three controls (two text box controls and one DataGridView control) to the same column in the DataSet.
The example shows how to handle the BindingComplete event and ensure that when the text value of a text box changes, other text boxes and DataGridView controls are updated with the correct values.
The specific code is as follows:
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 Loc ation, 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, E nd the current edit. E.binding.bindingmanagerbase.endcurrentedit ();}
The following code shows how to use the BindingSource component to share binding data across forms, with the following code:
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 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& Gt;<artist>dave matthews≪/artist> "+" <cd>under the Table and dreaming</cd> "+" <releasedate>1994</relea Sedate><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></recordin G> "+" <recording><artist>U2</artist> "+" <cd>joshua tree</cd><release Date>1987</releasedate> "+" <rating>5</rating></recording> "+" <recording> ;<artist>u2</artist> "+" <cd>how to dismantle an Atomic bomb</cd> "+" <released Ate>2004</releasedate><rating>4.5</rating></recording> "+" <recording><artist>natalie merchant</artist > "+" <cd>Tigerlily</cd><releaseDate>1995</releaseDate> "+" <RATING>3.5&L T;/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 forms//remain synchronized. private void Bindingsource1_bindingcomplete (object sender, BindingCompleteEventArgs e) {if (E.bindingcompletecont ext = = Bindingcompletecontext.datasourceupdate && e.exception = = null) e.binding.bindingmanagerbase. Endcurrentedit (); }//The detailed form would 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}); } }}
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Application of BindingSource method for data synchronization
This address: http://www.paobuke.com/develop/c-develop/pbk23598.html
Related content C # Use the Windows service to send messages using Newtonsoft to convert a JSON string to an object (in detail) C # picture Scaled instance the TV station program interface calling code based on C #
C # Implementing a custom dictionary class instance analysis of C # selection method Sorting example C # Multithreading Learning (ii) How to manipulate a thread DataGridView tips for working with C #
C # Application of BindingSource method for data synchronization