ControlBindingsCollection.Add 方法

定義

Binding 加入集合。

多載

Add(Binding)

將指定的 Binding 加入至集合。

Add(String, Object, String)

使用指定的控制項屬性名稱、資料來源和資料成員,來建立 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

要加入的 Binding

例外狀況

binding 為 Null。

控制項屬性已經資料繫結。

-或-

Binding 並未指定 DataSource 的有效資料行。

範例

下列程式碼範例會 Binding 建立 實例,並使用 Add 方法將 實例新增至 ControlBindingsCollection 控制項的 TextBox

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 這個多載所建立的 BindingAdd 屬性會設定為 屬性的值 DefaultDataSourceUpdateMode

變更完成時,就會 CollectionChanged 發生此事件。

適用於

Add(String, Object, String)

使用指定的控制項屬性名稱、資料來源和資料成員,來建立 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

要繫結之控制項屬性的名稱。

dataSource
Object

Object,代表資料來源。

dataMember
String

要繫結的屬性或清單。

傳回

新建立的 Binding

例外狀況

bindingnull

propertyName 已經資料繫結。

-或-

dataMember 並未指定 dataSource 的有效成員。

範例

下列程式碼範例會 Add 使用 方法,將三 BindingTextBox 物件新增至 ControlBindingsCollection 控制項的 。 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 這個多載所建立的 BindingAdd 屬性會設定為 屬性的值 DefaultDataSourceUpdateMode

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

要繫結之控制項屬性的名稱。

dataSource
Object

Object,代表資料來源。

dataMember
String

要繫結的屬性或清單。

formattingEnabled
Boolean

true 表示要格式化顯示的資料,否則為 false

傳回

新建立的 Binding

例外狀況

控制項上沒有 propertyName 所指定的屬性。

-或-

指定的屬性是唯讀屬性。

如果已停用格式化,而 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

要繫結之控制項屬性的名稱。

dataSource
Object

Object,代表資料來源。

dataMember
String

要繫結的屬性或清單。

formattingEnabled
Boolean

true 表示要格式化顯示的資料,否則為 false

updateMode
DataSourceUpdateMode

其中一個 DataSourceUpdateMode 值。

傳回

新建立的 Binding

例外狀況

控制項上沒有 propertyName 所指定的屬性,或是此屬性是唯讀的。

-或-

指定的資料成員不在資料來源中。

-或-

指定的資料來源、資料成員或控制項屬性,與集合中的另一項繫結關聯。

備註

呼叫 方法會 AddCollectionChanged 引發 事件。

適用於

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

要繫結之控制項屬性的名稱。

dataSource
Object

Object,代表資料來源。

dataMember
String

要繫結的屬性或清單。

formattingEnabled
Boolean

true 表示要格式化顯示的資料,否則為 false

updateMode
DataSourceUpdateMode

其中一個 DataSourceUpdateMode 值。

nullValue
Object

當資料來源具有這個值時,繫結的屬性會設定為 DBNull

傳回

新建立的 Binding

例外狀況

控制項上沒有 propertyName 所指定的屬性,或是此屬性是唯讀的。

-或-

指定的資料成員不在資料來源中。

-或-

指定的資料來源、資料成員或控制項屬性,與集合中的另一項繫結關聯。

備註

呼叫 方法會 AddCollectionChanged 引發 事件。

適用於

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

要繫結之控制項屬性的名稱。

dataSource
Object

Object,代表資料來源。

dataMember
String

要繫結的屬性或清單。

formattingEnabled
Boolean

true 表示要格式化顯示的資料,否則為 false

updateMode
DataSourceUpdateMode

其中一個 DataSourceUpdateMode 值。

nullValue
Object

當資料來源具有這個值時,繫結的屬性會設定為 DBNull

formatString
String

指示如何顯示數值的一或多個格式規範字元。

傳回

新建立的 Binding

例外狀況

控制項上沒有 propertyName 所指定的屬性,或是此屬性是唯讀的。

-或-

指定的資料成員不在資料來源中。

-或-

指定的資料來源、資料成員或控制項屬性,與集合中的另一項繫結關聯。

備註

呼叫 方法會 AddCollectionChanged 引發 事件。

適用於

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

要繫結之控制項屬性的名稱。

dataSource
Object

Object,代表資料來源。

dataMember
String

要繫結的屬性或清單。

formattingEnabled
Boolean

true 表示要格式化顯示的資料,否則為 false

updateMode
DataSourceUpdateMode

其中一個 DataSourceUpdateMode 值。

nullValue
Object

當資料來源具有這個值時,繫結的屬性會設定為 DBNull

formatString
String

指示如何顯示數值的一或多個格式規範字元。

formatInfo
IFormatProvider

要覆寫預設格式化行為之 IFormatProvider 的實作。

傳回

新建立的 Binding

例外狀況

控制項上沒有 propertyName 所指定的屬性,或是此屬性是唯讀的。

-或-

指定的資料成員不在資料來源中。

-或-

指定的資料來源、資料成員或控制項屬性,與集合中的另一項繫結關聯。

備註

Add呼叫 方法會 CollectionChanged 引發 事件。

適用於