From a Windows Forms perspective, "Data Binding" is a general
mechanism to bind data to a user interface element (Control). There are two
broad types of data binding in windows forms: simple and complex.
Simple Binding
Simple binding is a general way to bind a property on
a user interface element (Control) to a property on an instance of a type
(object). For example, if a developer had an instance of a Customer type they
could bind the Customer "Name" property to the "Text" property of a TextBox. When
"binding" these two properties, changes to the TextBox.Text property will be
propagated to the Customer.Name property and changes to the Customer.Name
property will be propagated to the TextBox.Text property. Windows Forms simple
binding supports binding to any public or internal .NET Framework property.
/*******************************************************************
* Setup (using the Visual Studio Form designer):
*
* Add 3 TextBoxes to the Form (textBox1, textBox2 and textBox3)
* Add the code below to the Form.Load event
******************************************************************/
/*******************************************************************
* Create a customer instance (use the Customer type below)
******************************************************************/
Customer cust = new Customer(0, "Mr. Zero", 10.0M);
/*******************************************************************
* Bind 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 (VS 2005):
/*******************************************************************
* Setup (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 with the
* following Customer class.
******************************************************************/
public class Customer
{
/* Private variables */
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 API */
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 Binding
Complex binding is a way to bind a list based user
interface element (e.g. ComboBox, Grid) to a list of data instances (e.g.
DataTable). Like simple binding, complex binding is generally two-way in that
changes to the user interface element are propagated to the data list and
changes to the data list are propagated to the user interface element. Windows
Forms complex binding supports binding to data lists that support the IList interface
(or IEnumerable when using a BindingSource component).
Sample: Complex Binding (VS
2005) (VS Project: DataBinding Intro)
/*******************************************************************
* Setup (using the Visual Studio Form designer):
*
* Add a DataGridView to the Form (dataGridView1)
* Add the code below to the Form.Load event
******************************************************************/
/*******************************************************************
* Create a list of customers. The list instance is named blc.
* Note: Customer has properties ID, Name and Rate
******************************************************************/
BindingList<Customer> blc = new BindingList<Customer>();
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 the DataGridView to the list of Customers using Complex
* binding (customer business type is shown above).
******************************************************************/
this.dataGridView1.DataSource = blc;
Using Simple Binding with a List
Simple binding can take two forms: property to property
binding (the simple binding described above) as well as property to "property
on an item in a list" binding. Property to "property on an item in a list"
binding is identical in form to Property to Property binding with the exception
that the data source is a list of items rather than a single item (e.g. BindingList<Customer>
rather than Customer). When using simple binding to bind to a list, the
Windows Forms data binding runtime will bind the user interface property to a
property on an item contained by the list. By default, the runtime will bind
to the first item in the list (e.g. binds "TextBox.Text" to
Customers[0].Name). If this same list is bound to another control using
complex binding (see below), the windows forms runtime will automatically synchronize
the simple bound property with the currently selected item in the complex
control. For example, a developer can use simple binding to bind the "Text"
property on a TextBox to the "Name" property on a Customer list. They can then
use complex binding to bind the same list to a Grid control. When they do
this, as they select different items in the Grid control, the Text property on
the TextBox will automatically re-bind to the currently selected item in the
Grid (i.e. the TextBox will show the "Name" of the currently selected
Customer"). In Windows Forms, keeping different bound items synchronized is called
"currency management". The core Windows Forms currency management engine is
implemented by a type called "CurrencyManager".
Sample: Simple Binding to a
List (VS 2005) (VS Project: DataBinding Intro)
/*******************************************************************
* Setup (using the Visual Studio Form designer):
*
* Add a DataGridView to the Form (dataGridView1)
* Add 3 TextBoxes to the Form (textBox1, textBox2 and textBox3)
* Add the code below to the Form.Load event
******************************************************************/
/*******************************************************************
* Create a list of customers. The list instance is named blc.
* Note: Customer has properties ID, Name and Rate
******************************************************************/
BindingList<Customer> blc = new BindingList<Customer>();
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 the DataGridView to the list of Customers using Complex
* binding (customer business type is shown above).
******************************************************************/
this.dataGridView1.DataSource = blc;
/*******************************************************************
* Bind the business object list to the TextBoxes. This uses simple
* binding of the form "binding to a property on an item 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);