欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#应用BindingSource实现数据同步的方法

程序员文章站 2024-02-11 20:51:58
本文以实例形式讲述了c#应用bindingsource实现数据同步的方法,对c#数据库程序开发来说具有一定的参考借鉴价值。具体实现方法如下: 下面的代码示例演示如何使用...

本文以实例形式讲述了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#程序设计有所帮助。