Exportar (0) Imprimir
Expandir todo

BindingsCollection (Clase)

Representa una colección de objetos Binding de un control.

Espacio de nombres: System.Windows.Forms
Ensamblado: System.Windows.Forms (en system.windows.forms.dll)

public class BindingsCollection : BaseCollection
public class BindingsCollection extends BaseCollection
public class BindingsCollection extends BaseCollection
No aplicable.

Un enlace de datos simple se consigue agregando objetos Binding a BindingsCollection. Cualquier objeto que hereda de la clase Control puede tener acceso a BindingsCollection mediante la propiedad DataBindings. Para obtener una lista de los controles de Windows que admiten enlaces de datos, vea la clase Binding.

En el siguiente ejemplo, se enlaza la propiedad Text de un control TextBox a un campo de una base de datos.

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

Los miembros estáticos públicos (Shared en Visual Basic) de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0
Mostrar:
© 2014 Microsoft