BindingsCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示控制項的 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
備註
簡單的資料系結是藉由將 物件加入 Binding 至 BindingsCollection 來完成。 繼承自 Control 類別的任何物件都可以透過 DataBindings 屬性存取 BindingsCollection 。 如需支援資料系結的 Windows 控制項清單,請參閱 類別 Binding 。
屬性
Count |
取得集合中的繫結總數。 |
IsReadOnly |
取得值,表示集合是否為唯讀。 (繼承來源 BaseCollection) |
IsSynchronized |
取得值,表示對 ICollection 的存取是否同步化。 (繼承來源 BaseCollection) |
Item[Int32] |
取得指定索引處的 Binding。 |
List |
取得集合中當做物件的繫結。 |
SyncRoot |
取得可用以同步存取 BaseCollection 的物件。 (繼承來源 BaseCollection) |
方法
事件
CollectionChanged |
發生於集合變更時。 |
CollectionChanging |
發生於集合將要變更時。 |
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別來篩選 IEnumerable 的項目。 |
AsParallel(IEnumerable) |
啟用查詢的平行化作業。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |