指定したコントロール プロパティ名、データ ソース、およびデータ メンバを使用して Binding を作成し、コレクションに追加します。
Overloads Public Function Add( _
ByVal propertyName As String, _ ByVal dataSource As Object, _ ByVal dataMember As String _) As Binding
[C#]
public Binding Add(stringpropertyName,objectdataSource,stringdataMember);
[C++]
public: Binding* Add(String* propertyName,Object* dataSource,String* dataMember);
[JScript]
public function Add(
propertyName : String,dataSource : Object,dataMember : String) : Binding;
パラメータ
- propertyName
バインドするコントロール プロパティの名前。 - dataSource
データ ソースを表す Object 。 - dataMember
バインド先のプロパティまたはリスト。
戻り値
新規作成した Binding 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | binding が null です。 |
Exception | propertyName が既にデータ連結されています。 |
Exception | dataMember が、 dataSource の有効なメンバを指定していません。 |
解説
Binding を追加すると、 CollectionChanged イベントが発生します。
使用例
[Visual Basic, C#, C++] Add メソッドを使用して、3 つの Binding オブジェクトを TextBox コントロールの ControlBindingsCollection に追加する例を次に示します。 ControlBindingsCollection には、 Control クラスの DataBindings プロパティを使用してアクセスします。
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
[C#]
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;
}
[C++]
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(S"Text", t, S"Text");
textBox1->DataBindings->Add(S"BackColor", t, S"BackColor");
textBox1->DataBindings->Add(S"ForeColor", t, S"ForeColor");
}
private:
DataTable* MakeTable()
{
/* Create a DataTable with three columns.
Two of the columns contain Color objects. */
DataTable* t = new DataTable(S"Control");
t->Columns->Add(S"BackColor", __typeof(Color));
t->Columns->Add(S"ForeColor", __typeof(Color));
t->Columns->Add(S"Text");
// Add three rows to the table.
DataRow* r;
r = t->NewRow();
r->Item[S"BackColor"] = __box(Color::Blue);
r->Item[S"ForeColor"] = __box(Color::Yellow);
r->Item[S"Text"] = S"Yellow on Blue";
t->Rows->Add(r);
r = t->NewRow();
r->Item[S"BackColor"] = __box(Color::White);
r->Item[S"ForeColor"] = __box(Color::Green);
r->Item[S"Text"] = S"Green on white";
t->Rows->Add(r);
r = t->NewRow();
r->Item[S"BackColor"] = __box(Color::Orange);
r->Item[S"ForeColor"] = __box(Color::Black);
r->Item[S"Text"] = S"Black on Orange";
t->Rows->Add(r);
return t;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
ControlBindingsCollection クラス | ControlBindingsCollection メンバ | System.Windows.Forms 名前空間 | ControlBindingsCollection.Add オーバーロードの一覧