ControlBindingsCollection.Add Method (String, Object, String)

 

Creates a Binding using the specified control property name, data source, and data member, and adds it to the collection.

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

public Binding Add(
	string propertyName,
	object dataSource,
	string dataMember
)

Parameters

propertyName
Type: System.String

The name of the control property to bind.

dataSource
Type: System.Object

An Object that represents the data source.

dataMember
Type: System.String

The property or list to bind to.

Return Value

Type: System.Windows.Forms.Binding

The newly created Binding.

Exception Condition
ArgumentNullException

The binding is null.

Exception

The propertyName is already data-bound.

Exception

The dataMember doesn't specify a valid member of the dataSource.

The DataSourceUpdateMode property of the Binding created by this overload of the Add method is set to the value of the DefaultDataSourceUpdateMode property.

Adding a Binding causes the CollectionChanged event to occur.

The following code example uses the Add method to add three Binding objects to the ControlBindingsCollection of a TextBox control. The ControlBindingsCollection is accessed through the DataBindings property of the Control class.

private void BindTextBoxProperties()
{
   // Clear the collection before adding new Binding objects.
   textBox1.DataBindings.Clear();

   // Create a DataTable containing Color objects.
   DataTable t = MakeTable();

   /* Bind the Text, BackColor, and ForeColor properties
   to columns in the DataTable. */
   textBox1.DataBindings.Add("Text", t, "Text");
   textBox1.DataBindings.Add("BackColor", t, "BackColor");
   textBox1.DataBindings.Add("ForeColor", t, "ForeColor");
}

private DataTable MakeTable()
{
   /* Create a DataTable with three columns.
   Two of the columns contain Color objects. */

   DataTable t = new DataTable("Control");
   t.Columns.Add("BackColor", typeof(Color));
   t.Columns.Add("ForeColor", typeof(Color));
   t.Columns.Add("Text");

   // Add three rows to the table.
   DataRow r;

   r = t.NewRow();
   r["BackColor"] = Color.Blue;
   r["ForeColor"] = Color.Yellow;
   r["Text"] = "Yellow on Blue";
   t.Rows.Add(r);

   r = t.NewRow();
   r["BackColor"] = Color.White;
   r["ForeColor"] = Color.Green;
   r["Text"] = "Green on white";
   t.Rows.Add(r);

   r = t.NewRow();
   r["BackColor"] = Color.Orange;
   r["ForeColor"] = Color.Black;
   r["Text"] = "Black on Orange";
   t.Rows.Add(r);

   return t;
}

.NET Framework
Available since 1.1
Return to top
Show: