Export (0) Print
Expand All

ControlBindingsCollection.Add Method

Adds a Binding to the collection.

Overload List

Adds the specified Binding to the collection.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shadows Sub Add(Binding)
[C#] public new void Add(Binding);
[C++] public: void Add(Binding*);
[JScript] public hide function Add(Binding);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Add(String, Object, String) As Binding
[C#] public Binding Add(string, object, string);
[C++] public: Binding* Add(String*, Object*, String*);
[JScript] public function Add(String, Object, String) : Binding;

Example

[Visual Basic, C#, C++] The following 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.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Add. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub BindTextBoxProperties()
    ' Clear the collection before adding new Binding objects.
    textBox1.DataBindings.Clear()
    
    ' Create a DataTable containing Color objects.
    Dim t As DataTable = 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")
End Sub    

Private Function MakeTable() As DataTable
    ' Create a DataTable with three columns.
    ' Two of the columns contain Color objects. 
    
    Dim t As New DataTable("Control")
    t.Columns.Add("BackColor", GetType(Color))
    t.Columns.Add("ForeColor", GetType(Color))
    t.Columns.Add("Text")
    
    ' Add three rows to the table.
    Dim r As DataRow
    
    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
End Function

[C#] 
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;
}

[C++] 
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(S"Text", t, S"Text");
   textBox1->DataBindings->Add(S"BackColor", t, S"BackColor");
   textBox1->DataBindings->Add(S"ForeColor", t, S"ForeColor");
}

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

   DataTable* t = new DataTable(S"Control");
   t->Columns->Add(S"BackColor", __typeof(Color));
   t->Columns->Add(S"ForeColor", __typeof(Color));
   t->Columns->Add(S"Text");

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

   r = t->NewRow();
   r->Item[S"BackColor"] = __box(Color::Blue);
   r->Item[S"ForeColor"] = __box(Color::Yellow);
   r->Item[S"Text"] = S"Yellow on Blue";
   t->Rows->Add(r);

   r = t->NewRow();
   r->Item[S"BackColor"] = __box(Color::White);
   r->Item[S"ForeColor"] = __box(Color::Green);
   r->Item[S"Text"] = S"Green on white";
   t->Rows->Add(r);

   r = t->NewRow();
   r->Item[S"BackColor"] = __box(Color::Orange);
   r->Item[S"ForeColor"] = __box(Color::Black);
   r->Item[S"Text"] = S"Black on Orange";
   t->Rows->Add(r);

   return t;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

ControlBindingsCollection Class | ControlBindingsCollection Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft