Export (0) Print
Expand All

Binding Constructor

.NET Framework 1.1

Initializes a new instance of the Binding class that simple-binds the specified control property to the specified data member of the specified data source.

[Visual Basic]
Public Sub New( _
   ByVal propertyName As String, _
   ByVal dataSource As Object, _
   ByVal dataMember As String _
)
[C#]
public Binding(
   string propertyName,
 object dataSource,
 string dataMember
);
[C++]
public: Binding(
   String* propertyName,
 Object* dataSource,
 String* dataMember
);
[JScript]
public function Binding(
   propertyName : String,
 dataSource : Object,
 dataMember : String
);

Parameters

propertyName
The name of the control property to bind.
dataSource
An Object that represents the data source.
dataMember
The property or list to bind to.

Exceptions

Exception Type Condition
Exception The propertyName is neither a valid property of a control nor an empty string ("").

Remarks

You can specify an instance of any of the following classes for the data source:

See the Binding class for more information about creating the dataMember string.

When you create a binding to a control's property, the new Binding object inspects the events exposed by the bound control and attaches to two particular events:

If you attempt to bind to a property that does not exist, a System.Windows.Forms.System.ArgumentException will be thrown when the Binding object is added to the control's Control.DataBindings collection.

Example

The following example binds a TextBox control to a table column in a DataSet named myDataSet. The example assumes you have declared myDataSet in the Declarations section of the module.

[Visual Basic] 
Private Sub CreateDataSet
   myDataSet = new DataSet("myDataSet")
   ' Populates the DataSet with tables, relations, and
   ' constraints.
End Sub

Private Sub BindTextBoxToDataSet 
   ' Binds a TextBox control to a column in the DataSet.
   textBox1.DataBindings.Add _
   ("Text", myDataSet, "Suppliers.CompanyName")
End Sub

[C#] 
private void CreateDataSet()
{
   myDataSet = new DataSet("myDataSet");
   /* Populates the DataSet with tables, relations, and 
      constraints. */
}

private void BindTextBoxToDataSet()
{
   /* Binds a TextBox control to a DataColumn named
   CompanyName in the DataTable named Suppliers. */
   textBox1.DataBindings.Add
   ("Text", myDataSet, "Suppliers.CompanyName");
}
   

[C++] 
private:
void CreateDataSet()
{
   myDataSet = new DataSet(S"myDataSet");
   /* Populates the DataSet with tables, relations, and 
      constraints. */
}

void BindTextBoxToDataSet()
{
   /* Binds a TextBox control to a DataColumn named
   CompanyName in the DataTable named Suppliers. */
   textBox1->DataBindings->Add
   (S"Text", myDataSet, S"Suppliers.CompanyName");
}
   

[JScript] 
private function CreateDataSet()
{
   myDataSet = new DataSet("myDataSet");
      
   // Creates a DataTable
   var myDataTable : DataTable = new DataTable("Suppliers");

   // Creates two columns, 
   var id : DataColumn = new DataColumn("ID", int);
   var name : DataColumn = new DataColumn("CompanyName", System.String);
   myDataTable.Columns.Add(id);
   myDataTable.Columns.Add(name);

   // Adds the table to the DataSet.
   myDataSet.Tables.Add(myDataTable);

   // Creates three suppliers in the Suppliers Table.
   var dr : DataRow;

   for(var i : int = 1; i < 4; i++)
   {
     dr = myDataTable.NewRow();
     dr[0] = i;
     dr[1] = "Company " + i.ToString();

     myDataTable.Rows.Add(dr);
   }   
}

private function BindTextBoxToDataSet()
{
   /* Binds a TextBox control to a DataColumn named
   CompanyName in the DataTable named Suppliers. */
   textBox1.DataBindings.Add
   (new Binding("Text", myDataSet, "Suppliers.CompanyName"));
}
   

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Binding Class | Binding Members | System.Windows.Forms Namespace | BindingsCollection | CurrencyManager

Show:
© 2015 Microsoft