Partager via


BindingsCollection Classe

Définition

Représente une collection d’objets Binding pour un contrôle.

public ref class BindingsCollection : System::Windows::Forms::BaseCollection
public class BindingsCollection : System.Windows.Forms.BaseCollection
type BindingsCollection = class
    inherit BaseCollection
Public Class BindingsCollection
Inherits BaseCollection
Héritage
Dérivé

Exemples

L’exemple suivant lie la Text propriété d’un TextBox contrôle à un champ d’une base de données.

private:
   void BindTextBoxControl()
   {
      DataSet^ myDataSet = gcnew DataSet;
      /* Insert code to populate the DataSet with tables, 
      columns, and data. */

      // Creates a new Binding object. 
      Binding^ myBinding = gcnew Binding(
         "Text",myDataSet,"customers.custToOrders.OrderAmount" );
      
      // Adds event delegates for the Parse and Format events.
      myBinding->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyToDecimal );
      myBinding->Format += gcnew ConvertEventHandler( this, &Form1::DecimalToCurrency );
      
      // Adds the new Binding to the BindingsCollection.
      text1->DataBindings->Add( myBinding );
   }

   void DecimalToCurrency( 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". */
      cevent->Value = safe_cast<Decimal ^>(cevent->Value)->ToString(  "c" );
   }

   void CurrencyToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      /* This method is the Parse event handler. The Parse event 
      occurs whenever the displayed value changes. The static 
      Parse method of the Decimal structure converts the 
      string back to its native Decimal type. */
      cevent->Value = Decimal::Parse( cevent->Value->ToString(),
         NumberStyles::Currency, nullptr );
   }
private void BindTextBoxControl()
{
   DataSet myDataSet = new DataSet();
   /* Insert code to populate the DataSet with tables, 
   columns, and data. */

   // Creates a new Binding object. 
   Binding myBinding = new Binding
   ("Text", myDataSet, "customers.custToOrders.OrderAmount");

   // Adds event delegates for the Parse and Format events.
   myBinding.Parse += new ConvertEventHandler(CurrencyToDecimal);
   myBinding.Format += new ConvertEventHandler(DecimalToCurrency);

   // Adds the new Binding to the BindingsCollection.
   text1.DataBindings.Add(myBinding);
}

private void DecimalToCurrency(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". */
   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

private void CurrencyToDecimal(object sender, 
   ConvertEventArgs cevent)
{   
   /* This method is the Parse event handler. The Parse event 
   occurs whenever the displayed value changes. The static 
   Parse method of the Decimal structure converts the 
   string back to its native Decimal type. */
   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}
Private Sub BindTextBoxControl()
   Dim myDataSet As New DataSet()
   ' Insert code to populate the DataSet with tables, columns, and data.

   ' Creates a new Binding object. 
   Dim myBinding As New Binding("Text", myDataSet, _
   "customers.custToOrders.OrderAmount")
   
   ' Adds event delegates for the Parse and Format events.
   AddHandler myBinding.Parse, AddressOf CurrencyToDecimal
   AddHandler myBinding.Format, AddressOf DecimalToCurrency
     
   ' Adds the new Binding to the BindingsCollection.
   text1.DataBindings.Add(myBinding)
End Sub 
    
Private Sub DecimalToCurrency(sender As Object, _
   cevent As ConvertEventArgs)
   ' 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". 
   cevent.Value = CDec(cevent.Value).ToString("c")
End Sub 
    
Private Sub CurrencyToDecimal(sender As Object, _
cevent As ConvertEventArgs)
   ' This method is the Parse event handler. The Parse event
   ' occurs whenever the displayed value changes. The static
   ' Parse method of the Decimal structure converts the 
   ' string back to its native Decimal type. 
   cevent.Value = Decimal.Parse(cevent.Value.ToString(), _
   NumberStyles.Currency, nothing)
   End Sub

Remarques

Une liaison de données simple est effectuée en ajoutant des Binding objets à un BindingsCollection. Tout objet qui hérite de la Control classe peut accéder à la BindingsCollection propriété via la DataBindings propriété. Pour obtenir la liste des contrôles Windows qui prennent en charge la liaison de données, consultez la Binding classe.

Propriétés

Nom Description
Count

Obtient le nombre total de liaisons dans la collection.

IsReadOnly

Obtient une valeur indiquant si la collection est en lecture seule.

(Hérité de BaseCollection)
IsSynchronized

Obtient une valeur indiquant si l’accès au fichier ICollection est synchronisé.

(Hérité de BaseCollection)
Item[Int32]

Obtient l’index Binding spécifié.

List

Obtient les liaisons de la collection en tant qu’objet.

SyncRoot

Obtient un objet qui peut être utilisé pour synchroniser l’accès BaseCollectionau .

(Hérité de BaseCollection)

Méthodes

Nom Description
Add(Binding)

Ajoute la liaison spécifiée à la collection.

AddCore(Binding)

Ajoute une Binding à la collection.

Clear()

Efface la collection d’objets de liaison.

ClearCore()

Efface la collection de tous les membres.

CopyTo(Array, Int32)

Copie tous les éléments de l’élément unidimensionnel Array actuel vers l’index de destination spécifié Array à partir de l’index de destination Array spécifié.

(Hérité de BaseCollection)
CreateObjRef(Type)

Crée un objet qui contient toutes les informations pertinentes requises pour générer un proxy utilisé pour communiquer avec un objet distant.

(Hérité de MarshalByRefObject)
Equals(Object)

Détermine si l’objet spécifié est égal à l’objet actuel.

(Hérité de Object)
GetEnumerator()

Obtient l’objet qui active l’itération via les membres de la collection.

(Hérité de BaseCollection)
GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetLifetimeService()
Obsolète.

Récupère l’objet de service de durée de vie actuel qui contrôle la stratégie de durée de vie de cette instance.

(Hérité de MarshalByRefObject)
GetType()

Obtient la Type de l’instance actuelle.

(Hérité de Object)
InitializeLifetimeService()
Obsolète.

Obtient un objet de service de durée de vie pour contrôler la stratégie de durée de vie de cette instance.

(Hérité de MarshalByRefObject)
MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
MemberwiseClone(Boolean)

Crée une copie superficielle de l’objet actuel MarshalByRefObject .

(Hérité de MarshalByRefObject)
OnCollectionChanged(CollectionChangeEventArgs)

Déclenche l’événement CollectionChanged.

OnCollectionChanging(CollectionChangeEventArgs)

Déclenche l’événement CollectionChanging.

Remove(Binding)

Supprime la liaison spécifiée de la collection.

RemoveAt(Int32)

Supprime la liaison de la collection à l’index spécifié.

RemoveCore(Binding)

Supprime le spécifié Binding de la collection.

ShouldSerializeMyAll()

Obtient une valeur qui indique si la collection doit être sérialisée.

ToString()

Retourne une chaîne qui représente l’objet actuel.

(Hérité de Object)

Événements

Nom Description
CollectionChanged

Se produit lorsque la collection a changé.

CollectionChanging

Se produit lorsque la collection est sur le point de changer.

Méthodes d’extension

Nom Description
AsParallel(IEnumerable)

Active la parallélisation d’une requête.

AsQueryable(IEnumerable)

Convertit un IEnumerable en IQueryable.

Cast<TResult>(IEnumerable)

Convertit les éléments d’un IEnumerable en type spécifié.

OfType<TResult>(IEnumerable)

Filtre les éléments d’une IEnumerable en fonction d’un type spécifié.

S’applique à

Voir aussi