ControlBindingsCollection.Add 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為收藏增添 a Binding 。
多載
| 名稱 | Description |
|---|---|
| Add(Binding) |
將指定的 Binding 資料加入集合。 |
| Add(String, Object, String) |
利用指定的控制屬性名稱、資料來源和資料成員建立 a Binding ,並將其加入集合。 |
| Add(String, Object, String, Boolean) |
建立包含指定控制屬性名稱、資料來源、資料成員及是否啟用格式資訊的綁定,並將綁定加入集合。 |
| Add(String, Object, String, Boolean, DataSourceUpdateMode) |
建立一個綁定,將指定的控制屬性綁定到指定資料來源的指定成員,並可選擇啟用格式化、根據指定的更新設定將值傳播到資料來源,並將綁定加入集合。 |
| Add(String, Object, String, Boolean, DataSourceUpdateMode, Object) |
建立一個綁定,將指定的控制屬性綁定到指定資料來源的指定資料成員,並可選擇啟用格式化、根據指定的更新設定將值傳播到資料來源、從資料來源回傳時 DBNull 將屬性設定為指定值,並將綁定加入集合。 |
| Add(String, Object, String, Boolean, DataSourceUpdateMode, Object, String) |
建立綁定,將指定的控制屬性綁定到指定資料來源的指定成員,並可選擇性啟用指定格式字串的格式化,根據指定的更新設定將值傳播到資料來源,從資料來源回傳時 DBNull 將屬性設定為指定值,並將綁定加入集合。 |
| Add(String, Object, String, Boolean, DataSourceUpdateMode, Object, String, IFormatProvider) |
建立綁定,將指定的控制屬性綁定至指定資料來源的指定資料成員,並可選擇性地啟用指定格式字串的格式化,根據指定的更新設定將值傳播至資料來源,從資料來源回傳時 DBNull 將屬性設定為指定值,設定指定的格式提供者, 並將裝訂加入收藏。 |
Add(Binding)
將指定的 Binding 資料加入集合。
public:
void Add(System::Windows::Forms::Binding ^ binding);
public void Add(System.Windows.Forms.Binding binding);
override this.Add : System.Windows.Forms.Binding -> unit
Public Sub Add (binding As Binding)
參數
例外狀況
是 binding 零。
範例
以下程式碼範例建立一個Binding實例,並使用該Add方法將實例加入控制項的TextBox實ControlBindingsCollection例。
protected:
void BindControls()
{
/* Create a new Binding using the DataSet and a
navigation path(TableName.RelationName.ColumnName).
Add event delegates for the Parse and Format events to
the Binding object, and add the object to the third
TextBox control's BindingsCollection. The delegates
must be added before adding the Binding to the
collection; otherwise, no formatting occurs until
the Current object of the BindingManagerBase for
the data source changes. */
Binding^ b = gcnew Binding(
"Text",ds,"customers.custToOrders.OrderAmount" );
b->Parse += gcnew ConvertEventHandler(
this, &Form1::CurrencyStringToDecimal );
b->Format += gcnew ConvertEventHandler(
this, &Form1::DecimalToCurrencyString );
textBox1->DataBindings->Add( b );
}
protected void BindControls()
{
/* Create a new Binding using the DataSet and a
navigation path(TableName.RelationName.ColumnName).
Add event delegates for the Parse and Format events to
the Binding object, and add the object to the third
TextBox control's BindingsCollection. The delegates
must be added before adding the Binding to the
collection; otherwise, no formatting occurs until
the Current object of the BindingManagerBase for
the data source changes. */
Binding b = new Binding
("Text", ds, "customers.custToOrders.OrderAmount");
b.Parse+=new ConvertEventHandler(CurrencyStringToDecimal);
b.Format+=new ConvertEventHandler(DecimalToCurrencyString);
textBox1.DataBindings.Add(b);
}
Protected Sub BindControls()
' Create a new Binding using the DataSet and a
' navigation path(TableName.RelationName.ColumnName).
' Add event delegates for the Parse and Format events to
' the Binding object, and add the object to the third
' TextBox control's BindingsCollection. The delegates
' must be added before adding the Binding to the
' collection; otherwise, no formatting occurs until
' the Current object of the BindingManagerBase for
' the data source changes.
Dim b As New Binding("Text", ds, "customers.custToOrders.OrderAmount")
AddHandler b.Parse, AddressOf CurrencyStringToDecimal
AddHandler b.Format, AddressOf DecimalToCurrencyString
textBox1.DataBindings.Add(b)
End Sub
備註
DataSourceUpdateMode由方法過載Add所產生的Binding屬性設定為該DefaultDataSourceUpdateMode屬性的值。
當變更完成時,事件 CollectionChanged 就會發生。
適用於
Add(String, Object, String)
利用指定的控制屬性名稱、資料來源和資料成員建立 a Binding ,並將其加入集合。
public:
System::Windows::Forms::Binding ^ Add(System::String ^ propertyName, System::Object ^ dataSource, System::String ^ dataMember);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string dataMember);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string? dataMember);
override this.Add : string * obj * string -> System.Windows.Forms.Binding
Public Function Add (propertyName As String, dataSource As Object, dataMember As String) As Binding
參數
- propertyName
- String
綁定控制屬性的名稱。
- dataMember
- String
綁定的屬性或清單。
傳回
新創造 Binding的。
例外狀況
binding 是 null。
範例
以下程式碼範例使用 Add 了將三個 Binding 物件加入 ControlBindingsCollection 控制項的方法 TextBox 。 是 ControlBindingsCollection 透過 DataBindings 類別的 Control 屬性來存取的。
private:
void BindTextBoxProperties()
{
// Clear the collection before adding new Binding objects.
textBox1->DataBindings->Clear();
// Create a DataTable containing Color objects.
DataTable^ t = MakeTable();
/* Bind the Text, BackColor, and ForeColor properties
to columns in the DataTable. */
textBox1->DataBindings->Add( "Text", t, "Text" );
textBox1->DataBindings->Add( "BackColor", t, "BackColor" );
textBox1->DataBindings->Add( "ForeColor", t, "ForeColor" );
}
DataTable^ MakeTable()
{
/* Create a DataTable with three columns.
Two of the columns contain Color objects. */
DataTable^ t = gcnew DataTable( "Control" );
t->Columns->Add( "BackColor", Color::typeid );
t->Columns->Add( "ForeColor", Color::typeid );
t->Columns->Add( "Text" );
// Add three rows to the table.
DataRow^ r;
r = t->NewRow();
r[ "BackColor" ] = Color::Blue;
r[ "ForeColor" ] = Color::Yellow;
r[ "Text" ] = "Yellow on Blue";
t->Rows->Add( r );
r = t->NewRow();
r[ "BackColor" ] = Color::White;
r[ "ForeColor" ] = Color::Green;
r[ "Text" ] = "Green on white";
t->Rows->Add( r );
r = t->NewRow();
r[ "BackColor" ] = Color::Orange;
r[ "ForeColor" ] = Color::Black;
r[ "Text" ] = "Black on Orange";
t->Rows->Add( r );
return t;
}
private void BindTextBoxProperties()
{
// Clear the collection before adding new Binding objects.
textBox1.DataBindings.Clear();
// Create a DataTable containing Color objects.
DataTable t = MakeTable();
/* Bind the Text, BackColor, and ForeColor properties
to columns in the DataTable. */
textBox1.DataBindings.Add("Text", t, "Text");
textBox1.DataBindings.Add("BackColor", t, "BackColor");
textBox1.DataBindings.Add("ForeColor", t, "ForeColor");
}
private DataTable MakeTable()
{
/* Create a DataTable with three columns.
Two of the columns contain Color objects. */
DataTable t = new DataTable("Control");
t.Columns.Add("BackColor", typeof(Color));
t.Columns.Add("ForeColor", typeof(Color));
t.Columns.Add("Text");
// Add three rows to the table.
DataRow r;
r = t.NewRow();
r["BackColor"] = Color.Blue;
r["ForeColor"] = Color.Yellow;
r["Text"] = "Yellow on Blue";
t.Rows.Add(r);
r = t.NewRow();
r["BackColor"] = Color.White;
r["ForeColor"] = Color.Green;
r["Text"] = "Green on white";
t.Rows.Add(r);
r = t.NewRow();
r["BackColor"] = Color.Orange;
r["ForeColor"] = Color.Black;
r["Text"] = "Black on Orange";
t.Rows.Add(r);
return t;
}
Private Sub BindTextBoxProperties()
' Clear the collection before adding new Binding objects.
textBox1.DataBindings.Clear()
' Create a DataTable containing Color objects.
Dim t As DataTable = MakeTable()
' Bind the Text, BackColor, and ForeColor properties
' to columns in the DataTable.
textBox1.DataBindings.Add("Text", t, "Text")
textBox1.DataBindings.Add("BackColor", t, "BackColor")
textBox1.DataBindings.Add("ForeColor", t, "ForeColor")
End Sub
Private Function MakeTable() As DataTable
' Create a DataTable with three columns.
' Two of the columns contain Color objects.
Dim t As New DataTable("Control")
t.Columns.Add("BackColor", GetType(Color))
t.Columns.Add("ForeColor", GetType(Color))
t.Columns.Add("Text")
' Add three rows to the table.
Dim r As DataRow
r = t.NewRow()
r("BackColor") = Color.Blue
r("ForeColor") = Color.Yellow
r("Text") = "Yellow on Blue"
t.Rows.Add(r)
r = t.NewRow()
r("BackColor") = Color.White
r("ForeColor") = Color.Green
r("Text") = "Green on white"
t.Rows.Add(r)
r = t.NewRow()
r("BackColor") = Color.Orange
r("ForeColor") = Color.Black
r("Text") = "Black on Orange"
t.Rows.Add(r)
Return t
End Function
備註
DataSourceUpdateMode由方法過載Add所產生的Binding屬性設定為該DefaultDataSourceUpdateMode屬性的值。
加入 a Binding 會導致 CollectionChanged 事件發生。
適用於
Add(String, Object, String, Boolean)
建立包含指定控制屬性名稱、資料來源、資料成員及是否啟用格式資訊的綁定,並將綁定加入集合。
public:
System::Windows::Forms::Binding ^ Add(System::String ^ propertyName, System::Object ^ dataSource, System::String ^ dataMember, bool formattingEnabled);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string dataMember, bool formattingEnabled);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string? dataMember, bool formattingEnabled);
override this.Add : string * obj * string * bool -> System.Windows.Forms.Binding
Public Function Add (propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean) As Binding
參數
- propertyName
- String
綁定控制屬性的名稱。
- dataMember
- String
綁定的屬性或清單。
- formattingEnabled
- Boolean
true格式化顯示資料;否則,。 false
傳回
新創造 Binding的。
例外狀況
如果格式被禁用,且 既 propertyName 不是控制項的有效屬性,也不是空字串(“”),
適用於
Add(String, Object, String, Boolean, DataSourceUpdateMode)
建立一個綁定,將指定的控制屬性綁定到指定資料來源的指定成員,並可選擇啟用格式化、根據指定的更新設定將值傳播到資料來源,並將綁定加入集合。
public:
System::Windows::Forms::Binding ^ Add(System::String ^ propertyName, System::Object ^ dataSource, System::String ^ dataMember, bool formattingEnabled, System::Windows::Forms::DataSourceUpdateMode updateMode);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string? dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode);
override this.Add : string * obj * string * bool * System.Windows.Forms.DataSourceUpdateMode -> System.Windows.Forms.Binding
Public Function Add (propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, updateMode As DataSourceUpdateMode) As Binding
參數
- propertyName
- String
綁定控制屬性的名稱。
- dataMember
- String
綁定的屬性或清單。
- formattingEnabled
- Boolean
true格式化顯示資料;否則,。 false
- updateMode
- DataSourceUpdateMode
這是其中一項 DataSourceUpdateMode 價值。
傳回
新創造 Binding的。
例外狀況
所 propertyName 給出的屬性在控制項上不存在,或是唯讀的。
-或-
指定的資料成員不存在於資料來源中。
-或-
指定的資料來源、資料成員或控制屬性會與集合中的另一個綁定相關聯。
備註
呼叫該Add方法會提升事件。CollectionChanged
適用於
Add(String, Object, String, Boolean, DataSourceUpdateMode, Object)
建立一個綁定,將指定的控制屬性綁定到指定資料來源的指定資料成員,並可選擇啟用格式化、根據指定的更新設定將值傳播到資料來源、從資料來源回傳時 DBNull 將屬性設定為指定值,並將綁定加入集合。
public:
System::Windows::Forms::Binding ^ Add(System::String ^ propertyName, System::Object ^ dataSource, System::String ^ dataMember, bool formattingEnabled, System::Windows::Forms::DataSourceUpdateMode updateMode, System::Object ^ nullValue);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode, object nullValue);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string? dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode, object? nullValue);
override this.Add : string * obj * string * bool * System.Windows.Forms.DataSourceUpdateMode * obj -> System.Windows.Forms.Binding
Public Function Add (propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, updateMode As DataSourceUpdateMode, nullValue As Object) As Binding
參數
- propertyName
- String
綁定控制屬性的名稱。
- dataMember
- String
綁定的屬性或清單。
- formattingEnabled
- Boolean
true格式化顯示資料;否則,。 false
- updateMode
- DataSourceUpdateMode
這是其中一項 DataSourceUpdateMode 價值。
傳回
新創造 Binding的。
例外狀況
所 propertyName 給出的屬性在控制項上不存在,或是唯讀的。
-或-
指定的資料成員不存在於資料來源中。
-或-
指定的資料來源、資料成員或控制屬性會與集合中的另一個綁定相關聯。
備註
呼叫該Add方法會提升事件。CollectionChanged
適用於
Add(String, Object, String, Boolean, DataSourceUpdateMode, Object, String)
建立綁定,將指定的控制屬性綁定到指定資料來源的指定成員,並可選擇性啟用指定格式字串的格式化,根據指定的更新設定將值傳播到資料來源,從資料來源回傳時 DBNull 將屬性設定為指定值,並將綁定加入集合。
public:
System::Windows::Forms::Binding ^ Add(System::String ^ propertyName, System::Object ^ dataSource, System::String ^ dataMember, bool formattingEnabled, System::Windows::Forms::DataSourceUpdateMode updateMode, System::Object ^ nullValue, System::String ^ formatString);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode, object nullValue, string formatString);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string? dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode, object? nullValue, string formatString);
override this.Add : string * obj * string * bool * System.Windows.Forms.DataSourceUpdateMode * obj * string -> System.Windows.Forms.Binding
Public Function Add (propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, updateMode As DataSourceUpdateMode, nullValue As Object, formatString As String) As Binding
參數
- propertyName
- String
綁定控制屬性的名稱。
- dataMember
- String
綁定的屬性或清單。
- formattingEnabled
- Boolean
true格式化顯示資料;否則,。 false
- updateMode
- DataSourceUpdateMode
這是其中一項 DataSourceUpdateMode 價值。
- formatString
- String
一個或多個格式指定字元,用以表示值的顯示方式。
傳回
新創造 Binding的。
例外狀況
所 propertyName 給出的屬性在控制項上不存在,或是唯讀的。
-或-
指定的資料成員不存在於資料來源中。
-或-
指定的資料來源、資料成員或控制屬性會與集合中的另一個綁定相關聯。
備註
呼叫該Add方法會提升事件。CollectionChanged
適用於
Add(String, Object, String, Boolean, DataSourceUpdateMode, Object, String, IFormatProvider)
建立綁定,將指定的控制屬性綁定至指定資料來源的指定資料成員,並可選擇性地啟用指定格式字串的格式化,根據指定的更新設定將值傳播至資料來源,從資料來源回傳時 DBNull 將屬性設定為指定值,設定指定的格式提供者, 並將裝訂加入收藏。
public:
System::Windows::Forms::Binding ^ Add(System::String ^ propertyName, System::Object ^ dataSource, System::String ^ dataMember, bool formattingEnabled, System::Windows::Forms::DataSourceUpdateMode updateMode, System::Object ^ nullValue, System::String ^ formatString, IFormatProvider ^ formatInfo);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode, object nullValue, string formatString, IFormatProvider formatInfo);
public System.Windows.Forms.Binding Add(string propertyName, object dataSource, string? dataMember, bool formattingEnabled, System.Windows.Forms.DataSourceUpdateMode updateMode, object? nullValue, string formatString, IFormatProvider? formatInfo);
override this.Add : string * obj * string * bool * System.Windows.Forms.DataSourceUpdateMode * obj * string * IFormatProvider -> System.Windows.Forms.Binding
Public Function Add (propertyName As String, dataSource As Object, dataMember As String, formattingEnabled As Boolean, updateMode As DataSourceUpdateMode, nullValue As Object, formatString As String, formatInfo As IFormatProvider) As Binding
參數
- propertyName
- String
綁定控制屬性的名稱。
- dataMember
- String
綁定的屬性或清單。
- formattingEnabled
- Boolean
true格式化顯示資料;否則,。 false
- updateMode
- DataSourceUpdateMode
這是其中一項 DataSourceUpdateMode 價值。
- formatString
- String
一個或多個格式指定字元,用以表示值的顯示方式。
- formatInfo
- IFormatProvider
一個用來覆寫預設格式行為的實作 IFormatProvider 。
傳回
新創造 Binding的。
例外狀況
所 propertyName 給出的屬性在控制項上不存在,或是唯讀的。
-或-
指定的資料成員不存在於資料來源中。
-或-
指定的資料來源、資料成員或控制屬性會與集合中的另一個綁定相關聯。
備註
呼叫該Add方法會提升事件。CollectionChanged