This documentation is archived and is not being maintained.

Binding Class

Represents the simple binding between the property value of an object and the property value of a control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public class Binding

Use the Binding class to create and maintain a simple binding between the property of a control and either the property of an object, or the property of the current object in a list of objects.

As an example of the first case, you can bind the Text property of a TextBox control to the FirstName property of a Customer object. As an example of the second case, you can bind the Text property of a TextBox control to the FirstName property of a DataTable that contains customers.

The Binding class also enables you to format values for display through the Format event and to retrieve formatted values through the Parse event.

When constructing a Binding instance with Binding constructor, you must specify three items:

  • The name of the control property to bind to.

  • The data source.

  • The navigation path that resolves to a list or property in the data source. The navigation path is also used to create the object's BindingMemberInfo property.

First, you must specify name of the control property you want to bind the data to. For example, to display data in a TextBox control, specify the Text property.

Second, you can specify an instance of any one of the classes in the following table as the data source.


C# example

Any class that implements IBindingList or ITypedList. These include the following: DataSet, DataTable, DataView, or DataViewManager.

DataSet ds = new DataSet("myDataSet");

Any class that implements IList to create an indexed collection of objects. The collection must be created and filled before creating the Binding. The objects in the list must all be of the same type; otherwise, an exception will be thrown.

ArrayList ar1 = new ArrayList;

Customer1 cust1 = new Customer("Louis");


A strongly typed IList of strongly typed objects

Customer [] custList = new Customer[3];

Third, you must specify the navigation path, which can be an empty string (""), a single property name, or a period-delimited hierarchy of names. If you set the navigation path to an empty string, the ToString method will be called on the underlying data source object.

If the data source is a DataTable, which can contain multiple DataColumn objects, the navigation path must be used to resolve to a specific column.


When the data source is a DataSet, DataViewManager, or DataTable, you are actually binding to a DataView. Consequently, the bound rows are actually DataRowView objects.

A period-delimited navigation path is required when the data source is set to an object that contains multiple DataTable objects (such as a DataSet or DataViewManager). You can also use a period-delimited navigation path when you bind to an object whose properties return references to other objects (such as a class with properties that return other class objects). For example, the following navigation paths all describe valid data fields:

  • "Size.Height"

  • "Suppliers.CompanyName"

  • "Regions.regionsToCustomers.CustomerFirstName"

  • "Regions.regionsToCustomers.customersToOrders.ordersToDetails.Quantity"

Each member of the path can return either a property that resolves to a single value (such as an integer), or a list of values (such as an array of strings). Although each member in the path can be a list or property, the final member must resolve to a property. Each member builds on the previous member: "Size.Height" resolves to the Height property for the current Size; "Regions.regionsToCustomers.CustomerFirstName" resolves to the first name for the current customer, where the customer is one of the customers for the current region.

A DataRelation returns a list of values by linking one DataTable to a second DataTable in a DataSet. If the DataSet contains DataRelation objects, you can specify the data member as a TableName followed by a RelationName, and then a ColumnName. For example, if the DataTable named "Suppliers" contains a DataRelation named "suppliers2products", the data member could be "Suppliers.suppliers2products.ProductName".

The data source can consist of a set of related classes. For example, imagine a set of classes that catalogs solar systems. The class named System contains a property named Stars that returns a collection of Star objects. Each Star object has Name and Mass properties, as well as a Planets property that returns a collection of Planet objects. In this system, each planet also has Mass and Name properties. Each Planet object further has a Moons property that returns a collection of Moon objects, each of which also has Name and Mass properties. If you specify a System object as the data source, you can specify any of the following as the data member:

  • "Stars.Name"

  • "Stars.Mass"

  • "Stars.Planets.Name"

  • "Stars.Planets.Mass"

  • "Stars.Planets.Moons.Name"

  • "Stars.Planets.Moons.Mass"

Controls that can be simple-bound feature a collection of Binding objects in a ControlBindingsCollection, which you can access through the control's DataBindings property. You add a Binding to the collection by calling the Add method, thereby binding a property of the control to a property of an object (or to a property of the current object in a list).

You can simple-bind to any object that derives from the System.Windows.Forms.Control class, for example, the following Windows controls:


Only the SelectedValue property of the ComboBox, CheckedListBox, and ListBox control is simple bound.

The BindingManagerBase class is an abstract class that manages all the Binding objects for a particular data source and data member. Classes that derive from BindingManagerBase are the CurrencyManager and the PropertyManager classes. How a Binding is managed depends on whether the Binding is a list binding or a property binding. For example, if it is a list binding, you can use the BindingManagerBase to specify a Position in the list; the Position, therefore, determines which item (out of all items in the list) is actually bound to a control. To return the appropriate BindingManagerBase, use the BindingContext.

To add a new row to a set of controls bound to the same DataSource, use the AddNew method of the BindingManagerBase class. Use the Item property of the BindingContext class to return the appropriate CurrencyManager. To escape the addition of the new row, use the CancelCurrentEdit method.

The following code example creates a Windows Form with several controls that demonstrate simple data binding. The example creates a DataSet with two tables named Customers and Orders, and a DataRelation named custToOrders. Four controls (a DateTimePicker and three TextBox controls) are data bound to columns in the tables. For each control, the example creates and adds a Binding to the control through the DataBindings property. The example returns a BindingManagerBase for each table through the form's BindingContext. Four Button controls increment or decrement the Position property on the BindingManagerBase objects.

using System;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
   private System.ComponentModel.Container components;
   private Button button1;
   private Button button2;
   private Button button3;
   private Button button4;
   private TextBox text1;
   private TextBox text2;
   private TextBox text3;

   private BindingManagerBase bmCustomers;
   private BindingManagerBase bmOrders;
   private DataSet ds;
   private DateTimePicker DateTimePicker1;

   public Form1()
      // Required for Windows Form Designer support.
      // Call SetUp to bind the controls.

   private void InitializeComponent()
      // Create the form and its controls. 
      this.components = new System.ComponentModel.Container();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.button3 = new System.Windows.Forms.Button();
      this.button4 = new System.Windows.Forms.Button();

      this.text1= new System.Windows.Forms.TextBox();
      this.text2= new System.Windows.Forms.TextBox();
      this.text3= new System.Windows.Forms.TextBox();

      this.DateTimePicker1 = new DateTimePicker();

      this.Text = "Binding Sample";
      this.ClientSize = new System.Drawing.Size(450, 200);

      button1.Location = new System.Drawing.Point(24, 16);
      button1.Size = new System.Drawing.Size(64, 24);
      button1.Text = "<";
      button1.Click+=new System.EventHandler(button1_Click);

      button2.Location = new System.Drawing.Point(90, 16);
      button2.Size = new System.Drawing.Size(64, 24);
      button2.Text = ">";
      button2.Click+=new System.EventHandler(button2_Click);

      button3.Location = new System.Drawing.Point(90, 100);
      button3.Size = new System.Drawing.Size(64, 24);
      button3.Text = "<";
      button3.Click+=new System.EventHandler(button3_Click);

      button4.Location = new System.Drawing.Point(150, 100);
      button4.Size = new System.Drawing.Size(64, 24);
      button4.Text = ">";
      button4.Click+=new System.EventHandler(button4_Click);

      text1.Location = new System.Drawing.Point(24, 50);
      text1.Size = new System.Drawing.Size(150, 24);

      text2.Location = new System.Drawing.Point(190, 50);
      text2.Size = new System.Drawing.Size(150, 24);

      text3.Location = new System.Drawing.Point(290, 150);
      text3.Size = new System.Drawing.Size(150, 24);

      DateTimePicker1.Location = new System.Drawing.Point(90, 150);
      DateTimePicker1.Size = new System.Drawing.Size(200, 800);


   protected override void Dispose( bool disposing ){
      if( disposing ){
         if (components != null){
      base.Dispose( disposing );
   public static void Main()
      Application.Run(new Form1());

   private void SetUp()
      // Create a DataSet with two tables and one relation.

   protected void BindControls()
      /* Create two Binding objects for the first two TextBox 
         controls. The data-bound property for both controls 
         is the Text property. The data source is a DataSet 
         (ds). The data member is the  
         "TableName.ColumnName" string. */
      text1.DataBindings.Add(new Binding
      ("Text", ds, "customers.custName"));
      text2.DataBindings.Add(new Binding
      ("Text", ds, "customers.custID"));

      /* Bind the DateTimePicker control by adding a new Binding. 
         The data member of the DateTimePicker is a 
         TableName.RelationName.ColumnName string. */
      Binding("Value", ds, "customers.CustToOrders.OrderDate"));

      /* Add event delegates for the Parse and Format events to a 
         new Binding object, and add the object to the third 
         TextBox control's BindingsCollection. The delegates 
         must be added before adding the Binding to the 
         collection; otherwise, no formatting occurs until 
         the Current object of the BindingManagerBase for 
         the data source changes. */
         Binding b = new Binding
         ("Text", ds, "customers.custToOrders.OrderAmount");
      b.Parse+=new ConvertEventHandler(CurrencyStringToDecimal);
      b.Format+=new ConvertEventHandler(DecimalToCurrencyString);

      // Get the BindingManagerBase for the Customers table. 
      bmCustomers = this.BindingContext [ds, "Customers"];

      /* Get the BindingManagerBase for the Orders table using the 
         RelationName. */ 
      bmOrders = this.BindingContext[ds, "customers.CustToOrders"];

   private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
      /* This method is the Format event handler. Whenever the 
         control displays a new value, the value is converted from 
         its native Decimal type to a string. The ToString method 
         then formats the value as a Currency, by using the 
         formatting character "c". */ 

      // The application can only convert to string type.  
      if(cevent.DesiredType != typeof(string)) return;

      cevent.Value = ((decimal) cevent.Value).ToString("c");

   private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
      /* This method is the Parse event handler. The Parse event 
         occurs whenever the displayed value changes. The static 
         ToDecimal method of the Convert class converts the 
         value back to its native Decimal type. */ 

      // Can only convert to decimal type. 
      if(cevent.DesiredType != typeof(decimal)) return;

      cevent.Value = Decimal.Parse(cevent.Value.ToString(),
      	NumberStyles.Currency, null);

      /* To see that no precision is lost, print the unformatted 
         value. For example, changing a value to "10.0001" 
         causes the control to display "10.00", but the 
         unformatted value remains "10.0001". */

   private void button1_Click(object sender, System.EventArgs e)
      // Go to the previous item in the Customer list.
      bmCustomers.Position -= 1;

   private void button2_Click(object sender, System.EventArgs e)
      // Go to the next item in the Customer list.
      bmCustomers.Position += 1;

   private void button3_Click(object sender, System.EventArgs e)
      // Go to the previous item in the Orders list.

   private void button4_Click(object sender, System.EventArgs e)
      // Go to the next item in the Orders list.

   // Create a DataSet with two tables and populate it. 
   private void MakeDataSet()
      // Create a DataSet.
      ds = new DataSet("myDataSet");

      // Create two DataTables.
      DataTable tCust = new DataTable("Customers");
      DataTable tOrders = new DataTable("Orders");

      // Create two columns, and add them to the first table.
      DataColumn cCustID = new DataColumn("CustID", typeof(int));
      DataColumn cCustName = new DataColumn("CustName");

      // Create three columns, and add them to the second table.
      DataColumn cID = 
         new DataColumn("CustID", typeof(int));
      DataColumn cOrderDate = 
         new DataColumn("orderDate",typeof(DateTime));
      DataColumn cOrderAmount = 
         new DataColumn("OrderAmount", typeof(decimal));

      // Add the tables to the DataSet.

      // Create a DataRelation, and add it to the DataSet.
      DataRelation dr = new DataRelation
      ("custToOrders", cCustID , cID);

      /* Populate the tables. For each customer and order, 
         create two DataRow variables. */
      DataRow newRow1;
      DataRow newRow2;

      // Create three customers in the Customers Table. 
      for(int i = 1; i < 4; i++)
         newRow1 = tCust.NewRow();
         newRow1["custID"] = i;
         // Add the row to the Customers table.
      // Give each customer a distinct name.
      tCust.Rows[0]["custName"] = "Alpha";
      tCust.Rows[1]["custName"] = "Beta";
      tCust.Rows[2]["custName"] = "Omega";

      // For each customer, create five rows in the Orders table. 
      for(int i = 1; i < 4; i++)
         for(int j = 1; j < 6; j++)
            newRow2 = tOrders.NewRow();
            newRow2["CustID"]= i;
            newRow2["orderDate"]= new DateTime(2001, i, j * 2);
            newRow2["OrderAmount"] = i * 10 + j  * .1;
            // Add the row to the Orders table.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0