C#应用BindingSource实现数据同步的方法
本文以实例形式讲述了C#应用BindingSource实现数据同步的方法,对C#数据库程序开发来说具有一定的参考借鉴价值。具体实现方法如下:
下面的代码示例演示如何使用BindingSource组件,将三个控件(两个文本框控件和一个DataGridView控件)绑定到DataSet中的同一列。
该示例演示如何处理BindingComplete事件,并确保当一个文本框的文本值更改时,会用正确的值更新其他文本框和DataGridView控件。
具体代码如下:
//Declarethecontrolstobeused. privateBindingSourcebindingSource1; privateTextBoxtextBox1; privateTextBoxtextBox2; privateDataGridViewdataGridView1; privatevoidInitializeControlsAndDataSource() { //Initializethecontrolsandsetlocation,sizeand //otherbasicproperties. this.dataGridView1=newDataGridView(); this.bindingSource1=newBindingSource(); this.textBox1=newTextBox(); this.textBox2=newTextBox(); this.dataGridView1.ColumnHeadersHeightSizeMode= DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Dock=DockStyle.Top; this.dataGridView1.Location=newPoint(0,0); this.dataGridView1.Size=newSize(292,150); this.textBox1.Location=newPoint(132,156); this.textBox1.Size=newSize(100,20); this.textBox2.Location=newPoint(12,156); this.textBox2.Size=newSize(100,20); this.ClientSize=newSize(292,266); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.dataGridView1); //DeclaretheDataSetandaddatableandcolumn. DataSetset1=newDataSet(); set1.Tables.Add("Menu"); set1.Tables[0].Columns.Add("Beverages"); //Addsomerowstothetable. set1.Tables[0].Rows.Add("coffee"); set1.Tables[0].Rows.Add("tea"); set1.Tables[0].Rows.Add("hotchocolate"); set1.Tables[0].Rows.Add("milk"); set1.Tables[0].Rows.Add("orangejuice"); //SetthedatasourcetotheDataSet. bindingSource1.DataSource=set1; //SettheDataMembertotheMenutable. bindingSource1.DataMember="Menu"; //Addthecontroldatabindings. dataGridView1.DataSource=bindingSource1; textBox1.DataBindings.Add("Text",bindingSource1, "Beverages",true,DataSourceUpdateMode.OnPropertyChanged); textBox2.DataBindings.Add("Text",bindingSource1, "Beverages",true,DataSourceUpdateMode.OnPropertyChanged); bindingSource1.BindingComplete+= newBindingCompleteEventHandler(bindingSource1_BindingComplete); } privatevoidbindingSource1_BindingComplete(objectsender,BindingCompleteEventArgse) { //Checkifthedatasourcehasbeenupdated,andthatnoerrorhasoccured. if(e.BindingCompleteContext== BindingCompleteContext.DataSourceUpdate&&e.Exception==null) //Ifnot,endthecurrentedit. e.Binding.BindingManagerBase.EndCurrentEdit(); }
下面的代码演示如何使用BindingSource组件跨窗体共享绑定数据,具体代码如下:
usingSystem; usingSystem.Drawing; usingSystem.Windows.Forms; usingSystem.Data; namespaceBindingSourceMultipleForms { publicclassMainForm:Form { publicMainForm() { this.Load+=newEventHandler(MainForm_Load); } privateBindingSourcebindingSource1; privateButtonbutton1; privatevoidMainForm_Load(objectsender,EventArgse) { InitializeData(); } privatevoidInitializeData() { bindingSource1=newSystem.Windows.Forms.BindingSource(); //HandletheBindingCompleteeventtoensurethetwoforms //remainsynchronized. bindingSource1.BindingComplete+= newBindingCompleteEventHandler(bindingSource1_BindingComplete); ClientSize=newSystem.Drawing.Size(292,266); DataSetdataset1=newDataSet(); //SomexmldatatopopulatetheDataSetwith. stringmusicXml= "<?xmlversion='1.0'encoding='UTF-8'?>"+ "<music>"+ "<recording><artist>DaveMatthews</artist>"+ "<cd>UndertheTableandDreaming</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>DaveMatthews</artist>"+ "<cd>LiveatRedRocks</cd>"+ "<releaseDate>1997</releaseDate><rating>4</rating></recording>"+ "<recording><artist>U2</artist>"+ "<cd>JoshuaTree</cd><releaseDate>1987</releaseDate>"+ "<rating>5</rating></recording>"+ "<recording><artist>U2</artist>"+ "<cd>HowtoDismantleanAtomicBomb</cd>"+ "<releaseDate>2004</releaseDate><rating>4.5</rating></recording>"+ "<recording><artist>NatalieMerchant</artist>"+ "<cd>Tigerlily</cd><releaseDate>1995</releaseDate>"+ "<rating>3.5</rating></recording>"+ "</music>"; //Readthexml. System.IO.StringReaderreader=newSystem.IO.StringReader(musicXml); dataset1.ReadXml(reader); //GetaDataViewofthetablecontainedinthedataset. DataTableCollectiontables=dataset1.Tables; DataViewview1=newDataView(tables[0]); //CreateaDataGridViewcontrolandaddittotheform. DataGridViewdatagridview1=newDataGridView(); 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"); //Createandaddabuttontotheform. button1=newButton(); button1.AutoSize=true; button1.Text="Show/EditDetails"; this.Controls.Add(button1); button1.Location=newPoint(50,200); button1.Click+=newEventHandler(button1_Click); } //HandletheBindingCompleteeventtoensurethetwoforms //remainsynchronized. privatevoidbindingSource1_BindingComplete(objectsender,BindingCompleteEventArgse) { if(e.BindingCompleteContext==BindingCompleteContext.DataSourceUpdate &&e.Exception==null) e.Binding.BindingManagerBase.EndCurrentEdit(); } //Thedetailedformwillbeshownwhenthebuttonisclicked. privatevoidbutton1_Click(objectsender,EventArgse) { DetailFormdetailForm=newDetailForm(bindingSource1); detailForm.Show(); } [STAThread] staticvoidMain() { Application.EnableVisualStyles(); Application.Run(newMainForm()); } } //Thedetailformclass. publicclassDetailForm:Form { privateBindingSourceformDataSource; //TheconstructortakesaBindingSourceobject. publicDetailForm(BindingSourcedataSource) { formDataSource=dataSource; this.ClientSize=newSize(240,200); TextBoxtextBox1=newTextBox(); this.Text="SelectionDetails"; textBox1.Width=220; TextBoxtextBox2=newTextBox(); TextBoxtextBox3=newTextBox(); TextBoxtextBox4=newTextBox(); textBox4.Width=30; textBox3.Width=50; //Associateeachtextboxwithacolumnfromthedatasource. 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=newPoint(10,10); textBox2.Location=newPoint(10,40); textBox3.Location=newPoint(10,80); textBox4.Location=newPoint(10,120); this.Controls.AddRange(newControl[]{textBox1,textBox2,textBox3,textBox4}); } } }
希望本文所述对大家的C#程序设计有所帮助。