Freigeben über


Control.DataBindings-Eigenschaft

Ruft die Datenbindungen für das Steuerelement ab.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public ReadOnly Property DataBindings As ControlBindingsCollection
'Usage
Dim instance As Control
Dim value As ControlBindingsCollection

value = instance.DataBindings
public ControlBindingsCollection DataBindings { get; }
public:
virtual property ControlBindingsCollection^ DataBindings {
    ControlBindingsCollection^ get () sealed;
}
/** @property */
public final ControlBindingsCollection get_DataBindings ()
public final function get DataBindings () : ControlBindingsCollection

Eigenschaftenwert

Eine ControlBindingsCollection, die die Binding-Objekte für das Steuerelement enthält.

Hinweise

Greifen Sie mit der DataBindings-Eigenschaft auf die ControlBindingsCollection zu. Wenn Sie der Auflistung Binding-Objekte hinzufügen, können Sie jede Eigenschaft eines Steuerelements an die Eigenschaft eines Objekts binden.

Beispiel

Im folgenden Codebeispiel werden einer ControlBindingsCollection, die aus fünf Steuerelementen besteht (vier TextBox-Steuerelementen und einem DateTimePicker-Steuerelement), Binding-Objekte hinzugefügt. Der Zugriff auf die ControlBindingsCollection erfolgt über die DataBindings-Eigenschaft der Control-Klasse.

Protected Sub 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 specified by a navigation 
    ' path in the form : TableName.ColumnName. 
    textBox1.DataBindings.Add _
       (New Binding("Text", ds, "customers.custName"))
    textBox2.DataBindings.Add _
       (New Binding("Text", ds, "customers.custID"))
    
    ' Bind the DateTimePicker control by adding a new Binding. 
    ' The data member of the DateTimePicker is specified by a 
    ' navigation path in the form: TableName.RelationName.ColumnName. 
    DateTimePicker1.DataBindings.Add _
       (New Binding("Value", ds, "customers.CustToOrders.OrderDate"))
    
    ' Create a new Binding using the DataSet and a 
    ' navigation path(TableName.RelationName.ColumnName).
    ' Add event delegates for the Parse and Format events to 
    ' the 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. 
    Dim b As New Binding("Text", ds, "customers.custToOrders.OrderAmount")
    AddHandler b.Parse, AddressOf CurrencyStringToDecimal
    AddHandler b.Format, AddressOf DecimalToCurrencyString
    textBox3.DataBindings.Add(b)
    
    ' Bind the fourth TextBox to the Value of the 
    ' DateTimePicker control. This demonstrates how one control
    ' can be bound to another.
    textBox4.DataBindings.Add("Text", DateTimePicker1, "Value")
    Dim bmText As BindingManagerBase = Me.BindingContext(DateTimePicker1)
    
    ' Print the Type of the BindingManagerBase, which is 
    ' a PropertyManager because the data source
    ' returns only a single property value. 
    Console.WriteLine(bmText.GetType().ToString())
    ' Print the count of managed objects, which is 1.
    Console.WriteLine(bmText.Count)
    
    ' Get the BindingManagerBase for the Customers table. 
    bmCustomers = Me.BindingContext(ds, "Customers")
    ' Print the Type and count of the BindingManagerBase.
    ' Because the data source inherits from IBindingList,
    ' it is a RelatedCurrencyManager (derived from CurrencyManager). 
    Console.WriteLine(bmCustomers.GetType().ToString())
    Console.WriteLine(bmCustomers.Count)
    
    ' Get the BindingManagerBase for the Orders of the current
    ' customer using a navigation path: TableName.RelationName. 
    bmOrders = Me.BindingContext(ds, "customers.CustToOrders")
End Sub    
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 specified by a navigation 
   path in the form : TableName.ColumnName. */
   textBox1.DataBindings.Add(new Binding
   ("Text", ds, "customers.custName"));
   textBox2.DataBindings.Add(new Binding
   ("Text", ds, "customers.custID"));
      
   /* Bind the DateTimePicker control by adding a new Binding. 
   The data member of the DateTimePicker is specified by a 
   navigation path in the form: TableName.RelationName.ColumnName. */
   DateTimePicker1.DataBindings.Add(new 
   Binding("Value", ds, "customers.CustToOrders.OrderDate"));

   /* Create a new Binding using the DataSet and a 
   navigation path(TableName.RelationName.ColumnName).
   Add event delegates for the Parse and Format events to 
   the 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);
   textBox3.DataBindings.Add(b);

   /*Bind the fourth TextBox to the Value of the 
   DateTimePicker control. This demonstrates how one control
   can be bound to another.*/
   textBox4.DataBindings.Add("Text", DateTimePicker1,"Value");
   BindingManagerBase bmText = this.BindingContext[
   DateTimePicker1];

   /* Print the Type of the BindingManagerBase, which is 
   a PropertyManager because the data source
   returns only a single property value. */
   Console.WriteLine(bmText.GetType().ToString());
   // Print the count of managed objects, which is 1.
   Console.WriteLine(bmText.Count);

   // Get the BindingManagerBase for the Customers table. 
   bmCustomers = this.BindingContext [ds, "Customers"];
   /* Print the Type and count of the BindingManagerBase.
   Because the data source inherits from IBindingList,
   it is a RelatedCurrencyManager (derived from CurrencyManager). */
   Console.WriteLine(bmCustomers.GetType().ToString());
   Console.WriteLine(bmCustomers.Count);
   
   /* Get the BindingManagerBase for the Orders of the current
   customer using a navigation path: TableName.RelationName. */ 
   bmOrders = this.BindingContext[ds, "customers.CustToOrders"];
}
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 specified by a navigation 
      path in the form : TableName.ColumnName. */
   textBox1->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custName" ) );
   textBox2->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custID" ) );
   
   /* Bind the DateTimePicker control by adding a new Binding. 
      The data member of the DateTimePicker is specified by a 
      navigation path in the form: TableName.RelationName.ColumnName. */
   DateTimePicker1->DataBindings->Add( gcnew Binding( "Value",ds,"customers.CustToOrders.OrderDate" ) );
   
   /* Create a new Binding using the DataSet and a 
      navigation path(TableName.RelationName.ColumnName).
      Add event delegates for the Parse and Format events to 
      the 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 = gcnew Binding( "Text",ds,"customers.custToOrders.OrderAmount" );
   b->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyStringToDecimal );
   b->Format += gcnew ConvertEventHandler( this, &Form1::DecimalToCurrencyString );
   textBox3->DataBindings->Add( b );
   
   /*Bind the fourth TextBox to the Value of the 
      DateTimePicker control. This demonstrates how one control
      can be bound to another.*/
   textBox4->DataBindings->Add( "Text", DateTimePicker1, "Value" );
   BindingManagerBase^ bmText = this->BindingContext[ DateTimePicker1 ];
   
   /* Print the Type of the BindingManagerBase, which is 
      a PropertyManager because the data source
      returns only a single property value. */
   Console::WriteLine( bmText->GetType() );
   
   // Print the count of managed objects, which is 1.
   Console::WriteLine( bmText->Count );
   
   // Get the BindingManagerBase for the Customers table. 
   bmCustomers = this->BindingContext[ds, "Customers"];
   
   /* Print the Type and count of the BindingManagerBase.
      Because the data source inherits from IBindingList,
      it is a RelatedCurrencyManager (derived from CurrencyManager). */
   Console::WriteLine( bmCustomers->GetType() );
   Console::WriteLine( bmCustomers->Count );
   
   /* Get the BindingManagerBase for the Orders of the current
      customer using a navigation path: TableName.RelationName. */
   bmOrders = this->BindingContext[ds, "customers.CustToOrders"];
}
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 specified by a navigation 
        path in the form : TableName.ColumnName. 
     */
    textBox1.get_DataBindings().
        Add(new Binding("Text", ds, "customers.custName"));
    textBox2.get_DataBindings().
        Add(new Binding("Text", ds, "customers.custID"));

    /*   Bind the DateTimePicker control by adding a new Binding. 
         The data member of the DateTimePicker is specified by a 
         navigation path in the form: TableName.RelationName.ColumnName. 
     */
    dateTimePicker1.get_DataBindings().
        Add(new Binding("Value", ds, 
        "customers.CustToOrders.OrderDate"));

    /*  Create a new Binding using the DataSet and a 
        navigation path(TableName.RelationName.ColumnName).
        Add event delegates for the Parse and Format events to 
        the 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.add_Parse(new ConvertEventHandler(CurrencyStringToDecimal));
    b.add_Format(new ConvertEventHandler(DecimalToCurrencyString));
    textBox3.get_DataBindings().Add(b);

    /*  Bind the fourth TextBox to the Value of the 
        DateTimePicker control. This demonstrates how one control
        can be bound to another.
     */
    textBox4.get_DataBindings().Add("Text", dateTimePicker1, "Value");
    BindingManagerBase bmText 
        = this.get_BindingContext().get_Item(dateTimePicker1);

    /*  Print the Type of the BindingManagerBase, which is 
        a PropertyManager because the data source
        returns only a single property value. 
     */
    Console.WriteLine(bmText.GetType().ToString());

    // Print the count of managed objects, which is 1.
    Console.WriteLine(bmText.get_Count());

    // Get the BindingManagerBase for the Customers table. 
    bmCustomers = this.get_BindingContext().get_Item(ds, "Customers");

    /*  Print the Type and count of the BindingManagerBase.
        Because the data source inherits from IBindingList,
        it is a RelatedCurrencyManager (derived from CurrencyManager). 
     */
    Console.WriteLine(bmCustomers.GetType().ToString());
    Console.WriteLine(bmCustomers.get_Count());

    /*  Get the BindingManagerBase for the Orders of the current
        customer using a navigation path: TableName.RelationName. 
     */
    bmOrders 
        = this.get_BindingContext().get_Item(ds, "customers.CustToOrders");
} //BindControls
protected function 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 specified by a navigation 
   path in the form : TableName.ColumnName. */
   textBox1.DataBindings.Add(new Binding
   ("Text", ds, "customers.custName"));
   textBox2.DataBindings.Add(new Binding
   ("Text", ds, "customers.custID"));
      
   /* Bind the DateTimePicker control by adding a new Binding. 
   The data member of the DateTimePicker is specified by a 
   navigation path in the form: TableName.RelationName.ColumnName. */
   DateTimePicker1.DataBindings.Add(new 
   Binding("Value", ds, "customers.CustToOrders.OrderDate"));

   /* Create a new Binding import the DataSet and a 
   navigation path(TableName.RelationName.ColumnName).
   Add event delegates for the Parse and Format events to 
   the 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. */
   var b : Binding = new Binding
   ("Text", ds, "customers.custToOrders.OrderAmount");
   b.add_Parse(CurrencyStringToDecimal);
   b.add_Format(DecimalToCurrencyString);
   textBox3.DataBindings.Add(b);

   /*Bind the fourth TextBox to the Value of the 
   DateTimePicker control. This demonstrates how one control
   can be bound to another.*/
   textBox4.DataBindings.Add("Text", DateTimePicker1,"Value");
   var bmText : BindingManagerBase = this.BindingContext[
   DateTimePicker1];

   /* Print the Type of the BindingManagerBase, which is 
   a PropertyManager because the data source
   returns only a single property value. */
   Console.WriteLine(bmText.GetType().ToString());
   // Print the count of managed objects, which is 1.
   Console.WriteLine(bmText.Count);

   // Get the BindingManagerBase for the Customers table. 
   bmCustomers = this.BindingContext [ds, "Customers"];
   /* Print the Type and count of the BindingManagerBase.
   Because the data source inherits from IBindingList,
   it is a RelatedCurrencyManager (derived from CurrencyManager). */
   Console.WriteLine(bmCustomers.GetType().ToString());
   Console.WriteLine(bmCustomers.Count);
   
   /* Get the BindingManagerBase for the Orders of the current
   customer import a navigation path: TableName.RelationName. */ 
   bmOrders = this.BindingContext[ds, "customers.CustToOrders"];
}

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Control-Klasse
Control-Member
System.Windows.Forms-Namespace
BindingContext-Klasse
Binding-Klasse
BindingManagerBase-Klasse