BindingsCollection Class
Represents a collection of Binding objects for a control.
For a list of all members of this type, see BindingsCollection Members.
System.Object
System.MarshalByRefObject
System.Windows.Forms.BaseCollection
System.Windows.Forms.BindingsCollection
System.Windows.Forms.ControlBindingsCollection
[Visual Basic] Public Class BindingsCollection Inherits BaseCollection [C#] public class BindingsCollection : BaseCollection [C++] public __gc class BindingsCollection : public BaseCollection [JScript] public class BindingsCollection extends BaseCollection
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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.
Example
[Visual Basic, C#, C++] The following example binds the Text property of a TextBox control to a field in a database.
[Visual Basic] 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 [C#] 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); } [C++] 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 (S"Text", myDataSet, S"customers.custToOrders.OrderAmount"); // Adds event delegates for the Parse and Format events. myBinding->Parse += new ConvertEventHandler(this, &Form1::CurrencyToDecimal); myBinding->Format += new 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 = __try_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 = __box(Decimal::Parse(cevent->Value->ToString(), NumberStyles::Currency, 0)); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Windows.Forms
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
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
See Also
BindingsCollection Members | System.Windows.Forms Namespace | CurrencyManager