다음을 통해 공유


BindingsCollection 클래스

컨트롤에 대한 Binding 개체의 컬렉션을 표시합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Class BindingsCollection
    Inherits BaseCollection
‘사용 방법
Dim instance As BindingsCollection
public class BindingsCollection : BaseCollection
public ref class BindingsCollection : public BaseCollection
public class BindingsCollection extends BaseCollection
public class BindingsCollection extends BaseCollection

설명

단순 데이터 바인딩은 BindingsCollectionBinding 개체를 추가하면 수행됩니다. Control 클래스에서 상속되는 모든 개체는 DataBindings 속성을 사용하여 BindingsCollection에 액세스할 수 있습니다. 데이터 바인딩을 지원하는 Windows 컨트롤 목록을 보려면 Binding 클래스를 참조하십시오.

예제

다음 예제에서는 TextBox 컨트롤의 Text 속성을 데이터베이스의 필드에 바인드합니다.

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 
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 = 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.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

상속 계층 구조

System.Object
   System.MarshalByRefObject
     System.Windows.Forms.BaseCollection
      System.Windows.Forms.BindingsCollection
         System.Windows.Forms.ControlBindingsCollection

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

BindingsCollection 멤버
System.Windows.Forms 네임스페이스
CurrencyManager