From an angle of Windows Form, "Data Binding" is a general mechanism that binds data to a user interface element (control). There are two data bind types in the Windows Form: Simple Binding and Complex Binding.
Simple binding
Simple Binding is a method of binding an attribute of a user interface element (control) to a type (object) instance. For example, if a developer has an instance of a Customer type, then he can bind the Customer's "name" property to a TextBox "text" property. After "Binding", after the change of TextBox's text attribute will "propagate" to the Custom's Name property, the change to the Custom's Name property will also "propagate" to TextBox's text property. The simple data binding of the Windows Form supports the .NET Framework property that is bound to any Public or Internal level.
Example: A simple data binding to a business object
/ ************************************************** ******************
* Settings (using VS form designers):
*
* Add 3 TextBox to Form (TextBox1, TextBox2 and TextBox3)
* Add the following code to the form.Load event processing method
*********************************************************** **************** /
/ ************************************************** ******************
* Create a Customer instance (using the Customer type below)
*********************************************************** **************** /
Customer Cust = New Customer (0, "Mr. Zero", 10.0M);
/ ************************************************** ******************
* Binding TextBox1, TextBox2 and TextBox3
*********************************************************** **************** /
This.TextBox1.databindings.add ("Text", Cust, "ID", TRUE);
This.TextBox2.databindings.add ("Text", Cust, "Name", true);
THIS.TEXTBOX2.DATABINDINGS.ADD ("Text", Cust, "Rate", True);
Customer business object definition:
/ ************************************************** ******************
* Settings (using the Visual Studio Form Designer):
*
* Add a new C # class file to your project and name it "Customer.cs"
* Replace the automatically generated Customer class code with the Customer class below.
*********************************************************** **************** /
Public Class Customer
{
/ * Private variable * /
PRIVATE INT_ID;
Private string _name;
Private decimal _rate;
/ * Constructor * / public customer ()
{
THIS.ID = -1;
THIS.NAME = STRING.EMPTY;
THIS.RATE = 0.0m;
}
Public Customer (int ID, String Name, Decimal Rate)
{
THIS.ID = ID;
THIS.NAME = Name;
THIS.RATE = RATE;
}
/ * Public property * /
Public Int ID
{
Get {return_id;}
Set {_id = value;}
}
Public String Name
{
Get {return _name;}
Set {_name = value;}
}
Public Decimal Rate
{
Get {return _rate;}
Set {_rate = value;}
}
}
Complex data binding
Complex data binding is a method of binding a list-based user interface element (such as ComboBox, Grid) to a data instance list (such as a DataTable). As with simple data binding, complex data binding is usually transmitted to a data list when the user interface element changes, and the data list is propagated to the user interface element. Windows Forms Complex Data Bind Support Binds to those data lists that support ILIST interfaces (or IENUMERABLE interfaces, if used by bindingsource components).
Example: Complex data binding (VS 2005)
/ ************************************************** ******************
* Settings (using the Visual Studio Form Designer):
*
* Add a DataGridVie to Form (DataGridView1)
* Add the following code to the form.Load event response method
*********************************************************** **************** /
/ ************************************************** ******************
* Create a list of Customer. This list instance is named BLC.
* Note: Customer has these properties: ID, Name and Rate
*********************************************************** **************** /
BindingList
Blc.add (New Customer (0, "Mr. Zero", 10.0M);
Blc.Add (New Customer (1, "Mrs. One", 15.0M));
Blc.Add (New Customer (2, "Dr. TWO", 20.0M));
/ ************************************************** ******************
* Bind DataGridView to Customer list with complex data binding
* Bind (Customer Service Type is displayed above).
*********************************************************** **************** /
THIS.DATAGRIDVIEW1.DATASOURCE = BLC;
Simple data binding for the list
Simple data binding There are two forms: the property is bound to the property (the binding of the previously described); the property is bound to the properties of a list of "a list of a list". The property is the same as the binding form of "a list of a list", the property is the same, except for the data source is an entry (Item) list rather than a single entry (like, BindingList
/ ************************************************** ******************
* Settings (using the Visual Studio Form Designer)
*
* Add a DataGridVie to Form (DataGridView1)
* Add 3 TextBox to Form (TextBox1, TextBox2 and TextBox3)
* Add the following code to the form.Load event response method
*********************************************************** **************** /
/ ************************************************** ******************
* Create a list of Customer. This list instance is named BLC
* Note: Customer has these properties: ID, Name and Rate
*********************************************************** **************** /
BindingList
Blc.add (New Customer (0, "Mr. Zero", 10.0M);
Blc.Add (New Customer (1, "Mrs. One", 15.0M));
Blc.Add (New Customer (2, "Dr. TWO", 20.0M));
/ ************************************************** *******************
Bind DataGridView to Customer list with complex data binding
* Bind (Customer Service Type is displayed above).
*********************************************************** **************** /
THIS.DATAGRIDVIEW1.DATASOURCE = BLC;
/ ************************************************** ******************
* Bind business objects list to textboxe. Use simple data binding here
* Bind to the properties of a list in a list
*********************************************************** **************** /
This.TextBox1.Databindings.add ("Text", BLC, "ID", TRUE);
This.TextBox2.Databindings.add ("Text", BLC, "Name", True);
This.TextBox3.Databindings.add ("Text", BLC, "Rate", True);
The Windows Form Data Binding Engine not only guarantees synchronization of properties and lists, but also provides some common services to help simplify this process. The data binding engine provides the following services:
Type conversion
If needed, the Windows Form will perform type conversion as part of the binding process. For example, if an attribute of a business object is integrated (such as Customer.ID) is bound to a string type of a control (such as TextBox.Text), data binding is running The integer and string values will be converted to each other. Windows Forms use TypeConvertable and iConvertible to perform type conversions.
format
Windows Forms Binding supports formatting target data using the .NET Framework formatted string. For example, when binding the property of a Decimal type of a business object (such as Order.total) to a control string type attribute (such as TextBox.Text), a formatted string (such as "c") can be specified, Therefore, this value can be displayed as a localized currency display form (such as ¥ 1.10), and the Windows Form is formatted by the IFORMATTABLE.
Example: Format
/ ************************************************** ******************
* Settings (using the Visual Studio Form Designer):
*
* Add 2 TextBoxe to Form (TextBox1 and TextBox2)
* Add the following code to the form.Load event response method
*********************************************************** **************** /
/ ************************************************** ******************
* Data source settings
*
* Create a table called NumBers and add 3 columns
* Id: int
* Name: String
* COST: DECIMAL
*********************************************************** **************** / DATATABLE_DT;
_dt = new datatable ("numbers");
_d.column.add ("id", typeof (int));
_d.columns.add ("name", typeof (string);
_d.columns.add ("cost", typeof (decimal));
_d.rows.add (0, "zero", 10.0m);
_d.Rows.Add (1, "one", 11.1m);
_d.Rows.Add (2, "Two", 12.2m);
/ ************************************************** ******************
* Bind textBox.text (string) to Numbers.ID (Integer)
* Type conversion between INT and String when binding runtime
*********************************************************** **************** /
THIS.TEXTBOX1.DATABINDINGS.ADD ("Text", _dt, "id", true);
/ ************************************************** ******************
* Bind textBox.text (string) to Numbers.cost
* The value will be displayed as a currency form when binding runtime (¥ value.00)
*
* Note: This is manually created to add binding and add it to the control of the control.
* Instead of using DataBindings.Add's overloading method.
*********************************************************** **************** /
Binding cb = new binding ("text", _dt, "cost", true);
/ * .NET Framework currency formatted string * /
Cb.FormatString = "c";
/ * Add Bind to TextBox * /
This.TextBox2.Databindings.add (cb);
Error handling
Windows Forms Data Binds are integrated with Windows Form ErrorProvider controls. When using ErrorProvider and if a simple data binding operation fails, the ERRORPROVIDER control will provide a representative of visual feedback (an error icon) on the user interface element. The ErrorProvider control will find an exception that occurred during the binding period, in addition, report iDataErrorinfo information on the data source that supports the iDataErrorInfo interface.
Example: Error handling
/ ************************************************** ******************
* Settings (using the Visual Studio Form Designer):
*
* Add 2 TextBoxe to Form (TextBox1 and TextBox2)
* Add an ErrorProvider to Form (ErrorProvider1)
* Add the following code to the form.Load event response method
*********************************************************** **************** /
/ ************************************************** ******************* Data Source Settings:
*
* Create a table called NumBers and add 3 columns
* Id: int
* Name: String
* COST: DECIMAL
*********************************************************** **************** /
DataTable_dt;
_dt = new datatable ("numbers");
_d.column.add ("id", typeof (int));
_d.columns.add ("name", typeof (string);
_d.columns.add ("cost", typeof (decimal));
_d.rows.add (0, "zero", 10.0m);
_d.Rows.Add (1, "one", 11.1m);
_d.Rows.Add (2, "Two", 12.2m);
/ ************************************************** ******************
* Set ERROMPROVIDER:
*
* Bind it to the same data source used by TextBox.
*********************************************************** **************** /
THIS.ErrorProvider1.datasource = _d;
/ ************************************************** ******************
* Bind textBox.text (string) to Numbers.ID (Integer)
* Type conversion between INT and String when binding runtime
*********************************************************** **************** /
THIS.TEXTBOX1.DATABINDINGS.ADD ("Text", _dt, "id", true);
/ ************************************************** ******************
* Bind textBox.text (string) to Numbers.cost
* The value will be displayed as a currency form when binding runtime (¥ value.00)
*
* Note: This is manually created to add binding and add it to the control of the control.
* Instead of using DataBindings.Add overload method
*********************************************************** **************** /
Binding cb = new binding ("text", _dt, "cost", true);
/ * .NET Framework currency formatted string * /
Cb.FormatString = "c";
/ * Add Bind to TextBox * /
This.TextBox2.Databindings.add (cb);
Currency Management and BindingContext
Currency Management
One of the most important services provided by Windows Form Data Binds is Currency Management. In the context binding in Windows Forms, Currency is just maintained and synchronized "current item" for a list. In a Windows Form, Currency Management is available through a type called "currencymanager", which is a subclass of an abstract class "bindingmanagerbase". CurrencyManager offers a set of events, including "currentchanged", "POSITIONCHANGED", controls, and developers to monitor changes in "Currency" through this set of events. In addition, CurrencyManager provides some properties to "position" and "current" to allow controls and developers to set up and retrieve the current item (location). A key aspect of the Windows Form Data Binding is that currency is not maintained by a control like DataGrid, but is maintained by an instance of a CurrencyManager shared by multiple controls. Binding context
When using a simple list bind (that is, the simple data binding control needs to synchronize "current item" in its data source. In order to do this, the binding needs to get the "current" item it associated with the CurrencyManager. Bind the "bindingContext" property of its control to get the CurrencyManager of its data source, a control is typically a bindingContext from its parent form. "BindingContext" is a single-form CurrenAger cache, more precisely, BindingContext is a cache of a BindingManagerBase instance, while the base class of CurrencyManager when BindingManagerbase is.
For example, assume that a developer has a Customer list, binds to a DataGrid control (DataGrid.DataSource is set to a Customer list). In addition, developers have a simple control, such as a TextBox, bind to the same list (Textbox is binding its text to the Name property in the Customer list), click one of the DataGrid DataGrid will make the clicked to become the current selection item. How did DataGrid do? DataGrid first access its bindingContext attribute to get its data source (list) BindingManagerBase ("BindingContext" Returns BindingManagerBase (if you don't exist), then DataGrid will change "Current "(It achieves the purpose by setting the position property of the CurrencyManager). When a simple data bind is constructed, the control will get the BindingManagerBase (CurreencyManager) associated with its data source, which will listen to the Change event on BindingManagerBase, and synchronize its tie when the "current item" of BindingManagerBase changes. Set attributes (such as a Text property). The correct synchronization is that both DataGrid and TextBox must use the same currencessManager (BindingManagerBase). If they use different CurrencyManager, the simple binding properties will not be updated correctly when the entry in the DataGrid changes. As mentioned earlier, the control (and developer) can use a control's BindingContext attribute to get BindingManagerBase associated with the data source. When requesting a BindingManagerBase from BindingContext, BindingContext will first find its cache to see if there is a requesting BindingManagerBase. If this BindingManagerBase is not present in the cache, BindingContext will create and return a new (and add it to cache). BindingContext typically for each form is a global (bindingContext for child control), therefore typically shared by BindingManagerBase (CURRENCYMANager). BindingContext has 2 forms:
/ * Get a bindingmanagerbase * / for a given data source
BMB = this.bindingContext [dataable];
/ * Get a bindingmanagerbase * / for a given data source and data
BMB = this.bindingContext [DataSet, "Numbers"];
The first form is often used to obtain a list of BindingManagerBases such as a list of DataTable like ADO.NET. The second form is used to obtain a bindingmanagerbase that contains a parent data source containing a sub-list (such as a Datatable Dataset). One of BindingContext is one of the most unfollows uses different forms to specify the same data source, which will result in two different BindingManagerBase instances. So far, this is the most common cause of control binding to the same data source but does not synchronize (control through bindingContext to get a bindingmanagerbase). Example: Control synchronization
/ * Create a DataSet * / with a DataTable
DataSet DataSet = New DataSet ();
DataSet.Tables.Add ("Numbers");
DataTable.columns.Add ("ID", TypeOf (int));
DataTable.Columns.Add ("Name", TypeOf (String));
DataTable.Rows.Add (0, "Zero");
DataTable.Rows.Add (1, "one");
/ ************************************************** ******************
* Bind the first DataGridView and TextBox to DataSet "NumBers" table
* This DataGridView will get a CurrencyManager for a data source using BindingContext
* DataGridView1 will use BindingContext in the following form
*
* BMB = BindingContext [DataSet, "Numbers"];
*
* TextBox1 text binding will also get a bindingmanagerbase, and use the following BindingContext form
*
* BMB = BindingContext [DataSet, "Number"];
*
* Therefore, DataGridView1 and TextBox1 will share the same bindingmanagerbase (currencessManager)
*
*********************************************************** ***************** /
THIS.DATAGRIDVIEW1.DATASOURCE = DataSet;
This.DataGridView1.datamember = "Numbers";
This.TextBox1.databindings.add ("Text", DataSet, "Numbers.Name", True);
/ ************************************************** ******************
* Variable "DATATABLE" also points to this "number" table. Although
* The above DataGridView and TextBox use "DataSource" and "DataMember" forms
* Bind to the table, can now be bound to the same table (and data), bind to "DataTable" directly, just as shown below
* When doing this, DataGridView2 will use BindingContext in the following form.
*
* BMB = BindingContext [dataable];
*
* TextBox12 text binding will also use the following BindingContext form *
* BMB = BindingContext [dataable];
*
* So DataGridView2 and TextBox2 will share the same BindingManagerBase (CurrencyManager)
* However, they will not share the same currencymanager as DataGridView1, TextBox1 because they use different forms.
* Take the binding of them.
*********************************************************** ***************** /
This.DataGridView2.datasource = DataTable;
This.TextBox2.Databindings.add ("Text", DataTable, "Name", True);
Control binding operation
Windows Form Simple Binding Type (System.Windows.Forms.Binding) Allows developer control how to update the binding data source attribute when the user interface element content changes, and how to change during data source properties When updating user interface elements. For example, if a developer binds the Name property of the Customer instance to the Text property of the TextBox control, the developer can specify when to "propagate" to the data source when the user interface element occurs. The currently supported options are: During the Validation of TextBox (when the user removes the focus out of TextBox - this is the default value); when the text value changes; and never update the data source. Developers can control when to update the data source to the bounded user interface element. The currently supported options is: When the data source property changes (default) and never update. Note that developers can combine "never update" and call binding API (READVALUE / WRITEVALUE) to provide data synchronization rules between their own displayed data sources and user interface elements.
Example: Displayed Bindings
/ ************************************************** ******************
* Settings (using the Visual Studio Form Designer):
*
* Add 3 TextBoxe to Form (TextBox1, TextBox2 and Textbox2)
* Add 1 ErrorProvider to Form (ErrorProvider1)
* Add a button button button to the form form (Button1)
* Add the following code to the form.Load event processing method
*********************************************************** **************** /
/ ************************************************** ******************
* Data source settings:
*
* Create a table called NumBers and add 3 columns
* Id: int
* Name: String
* COST: DECIMAL
*********************************************************** **************** /
DataTable_dt;
_dt = new datatable ("numbers");
_d.column.add ("id", typeof (int));
_d.columns.add ("name", typeof (string);
_d.columns.add ("cost", typeof (decimal)); _ Dt.Rows.Add (0, "Zero", 10.0m);
_d.Rows.Add (1, "one", 11.1m);
_d.Rows.Add (2, "Two", 12.2m);
/ ************************************************** ******************
* Set ERROMPROVIDER:
*
* Bind it to the same data source used by TextBox.
*********************************************************** **************** /
THIS.ErrorProvider1.datasource = _d;
/ ************************************************** ******************
* Bind textBox.text (string) to Numbers.ID (Integer)
*
* Set DataSourceUpdateMode is OnPropertyChanged. This will
* Causes the data source attribute now updates the data source attribute once the TextBox.Text changes.
*
* Note that in this mode ErrorProvider will immediately display an error instead of focusing on the user.
* Only one error is displayed after removing TextBox
*********************************************************** **************** /
This.TextBox1.databindings.add ("text", _dt, "id", true,
DataSourceUpdateMode.onPropertyChanged;
/ ************************************************** ******************
* Bind TextBox.text to Form.Size (under this scene, form form is a data source)
* Do not let control changes to the data source (form form)
*********************************************************** **************** /
Binding sizebinding = new binding ("text", this, "size", true,
DataSourceUpdateMode.never;
This.TextBox2.DATABINDINGS.ADD (SizeBinding);
/ ************************************************** ******************
* Set up the update data source (Form.Size) displayed by Button.Click.
*
* Use an anonymous agent to make your code more concise
*********************************************************** **************** /
This.button1.click = delegate (Object Button, Eventargs Args)
{
Sizebinding.writevalue ();
}