Export (0) Print
Expand All

BindingsCollection Class

Represents a collection of Binding objects for a control.

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

public class BindingsCollection : BaseCollection
public class BindingsCollection extends BaseCollection
public class BindingsCollection extends BaseCollection

Simple data binding is accomplished by adding Binding objects to a BindingsCollection. Any object that inherits from the Control class can access the BindingsCollection through the DataBindings property. For a list of Windows controls that support data binding, see the Binding class.

The following example binds the Text property of a TextBox control to a field in a database.

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 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.add_Parse(new ConvertEventHandler(CurrencyToDecimal));
    myBinding.add_Format(new ConvertEventHandler(DecimalToCurrency));

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

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.set_Value(((System.Decimal)(cevent.get_Value())).ToString("c"));
} //DecimalToCurrency

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.set_Value(Decimal.Parse(cevent.get_Value().ToString(),
        NumberStyles.Currency, null));
} //CurrencyToDecimal

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft