BindingsCollection 類別

定義

表示控制項的 Binding 物件集合。

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
繼承
衍生

範例

下列範例會將 Text 控制項的 TextBox 屬性系結至資料庫中的欄位。

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

備註

簡單的資料系結是藉由將 物件加入 BindingBindingsCollection 來完成。 繼承自 Control 類別的任何物件都可以透過 DataBindings 屬性存取 BindingsCollection 。 如需支援資料系結的 Windows 控制項清單,請參閱 類別 Binding

屬性

Count

取得集合中的繫結總數。

IsReadOnly

取得值,表示集合是否為唯讀。

(繼承來源 BaseCollection)
IsSynchronized

取得值,表示對 ICollection 的存取是否同步化。

(繼承來源 BaseCollection)
Item[Int32]

取得指定索引處的 Binding

List

取得集合中當做物件的繫結。

SyncRoot

取得可用以同步存取 BaseCollection 的物件。

(繼承來源 BaseCollection)

方法

Add(Binding)

將指定的繫結新增至集合中。

AddCore(Binding)

Binding 加入集合。

Clear()

清除繫結物件的集合。

ClearCore()

清除任何成員的集合。

CopyTo(Array, Int32)

將目前一維 Array 的所有元素複製到指定的一維 Array (從指定的目的 Array 索引開始)。

(繼承來源 BaseCollection)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

取得允許逐一查看集合之成員的物件。

(繼承來源 BaseCollection)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OnCollectionChanged(CollectionChangeEventArgs)

引發 CollectionChanged 事件。

OnCollectionChanging(CollectionChangeEventArgs)

引發 CollectionChanging 事件。

Remove(Binding)

從集合中刪除指定的繫結。

RemoveAt(Int32)

將指定索引處的繫結從集合中刪除。

RemoveCore(Binding)

從集合移除指定的 Binding

ShouldSerializeMyAll()

取得值,指出集合是否應該序列化。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

事件

CollectionChanged

發生於集合變更時。

CollectionChanging

發生於集合將要變更時。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱