Language

BindingContext.Item[] プロパティ

定義

BindingManagerBaseを取得します。

オーバーロード

名前 説明
Item[Object, String]

指定したデータ ソースとデータ メンバーに関連付けられている BindingManagerBase を取得します。

Item[Object]

指定したデータ ソースに関連付けられている BindingManagerBase を取得します。

Item[Object, String]

ソース:
BindingContext.cs
ソース:
BindingContext.cs
ソース:
BindingContext.cs
ソース:
BindingContext.cs
ソース:
BindingContext.cs

指定したデータ ソースとデータ メンバーに関連付けられている BindingManagerBase を取得します。

public:
 property System::Windows::Forms::BindingManagerBase ^ default[System::Object ^, System::String ^] { System::Windows::Forms::BindingManagerBase ^ get(System::Object ^ dataSource, System::String ^ dataMember); };
public System.Windows.Forms.BindingManagerBase this[object dataSource, string dataMember] { get; }
public System.Windows.Forms.BindingManagerBase this[object dataSource, string? dataMember] { get; }
member this.Item(obj * string) : System.Windows.Forms.BindingManagerBase
Default Public ReadOnly Property Item(dataSource As Object, dataMember As String) As BindingManagerBase

パラメーター

dataSource
Object

特定の BindingManagerBaseに関連付けられているデータ ソース。

dataMember
String

特定の BindingManagerBaseに解決される情報を含むナビゲーション パス。

プロパティ値

指定したデータ ソースとデータ メンバーの BindingManagerBase 。

例外

指定した dataMember がデータ ソース内に存在しません。

例

次のコード例では、 Item[] を使用して特定のバインドの BindingManagerBase を取得する方法を示します。 また、BindingCompleteのBindingManagerBase イベントを処理して、コントロール値のいずれかが変更されたときに、同じデータ ソースにバインドされた複数のコントロールが確実に同期されるようにする方法についても説明します。 この例を実行するには、コードを Windows フォームに貼り付け、フォームのコンストラクターまたはイベント処理メソッドから InitializeControlsAndData メソッド Load 呼び出します。

private void InitializeControlsAndData()
{
    // Initialize the controls and set location, size and 
    // other basic properties.
    this.dataGridView1 = new DataGridView();
    
    this.textBox1 = new TextBox();
    this.textBox2 = new TextBox();
    this.dataGridView1.ColumnHeadersHeightSizeMode =
        DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dataGridView1.Dock = DockStyle.Top;
    this.dataGridView1.Location = new Point(0, 0);
    this.dataGridView1.Size = new Size(292, 150);
    this.textBox1.Location = new Point(132, 156);
    this.textBox1.Size = new Size(100, 20);
    this.textBox2.Location = new Point(12, 156);
    this.textBox2.Size = new Size(100, 20);
    this.ClientSize = new Size(292, 266);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.dataGridView1);

    // Declare the DataSet and add a table and column.
    DataSet set1 = new DataSet();
    set1.Tables.Add("Menu");
    set1.Tables[0].Columns.Add("Beverages");

    // Add some rows to the table.
    set1.Tables[0].Rows.Add("coffee");
    set1.Tables[0].Rows.Add("tea");
    set1.Tables[0].Rows.Add("hot chocolate");
    set1.Tables[0].Rows.Add("milk");
    set1.Tables[0].Rows.Add("orange juice");

    // Add the control data bindings.
    dataGridView1.DataSource = set1;
    dataGridView1.DataMember = "Menu";
    textBox1.DataBindings.Add("Text", set1,
        "Menu.Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
    textBox2.DataBindings.Add("Text", set1,
        "Menu.Beverages", true, DataSourceUpdateMode.OnPropertyChanged);

    BindingManagerBase bmb = this.BindingContext[set1, "Menu"];
    bmb.BindingComplete += new BindingCompleteEventHandler(bmb_BindingComplete);
}

private void bmb_BindingComplete(object sender, BindingCompleteEventArgs e)
{
    // Check if the data source has been updated, and that no error has occurred.
    if (e.BindingCompleteContext ==
        BindingCompleteContext.DataSourceUpdate && e.Exception == null)

        // If not, end the current edit.
        e.Binding.BindingManagerBase.EndCurrentEdit(); ;
}
Dim WithEvents bmb As BindingManagerBase

Private Sub InitializeControlsAndData() 
    ' Initialize the controls and set location, size and 
    ' other basic properties.
    Me.dataGridView1 = New DataGridView()
    
    Me.textBox1 = New TextBox()
    Me.textBox2 = New TextBox()
    Me.dataGridView1.ColumnHeadersHeightSizeMode = _
        DataGridViewColumnHeadersHeightSizeMode.AutoSize
    Me.dataGridView1.Dock = DockStyle.Top
    Me.dataGridView1.Location = New Point(0, 0)
    Me.dataGridView1.Size = New Size(292, 150)
    Me.textBox1.Location = New Point(132, 156)
    Me.textBox1.Size = New Size(100, 20)
    Me.textBox2.Location = New Point(12, 156)
    Me.textBox2.Size = New Size(100, 20)
    Me.ClientSize = New Size(292, 266)
    Me.Controls.Add(Me.textBox2)
    Me.Controls.Add(Me.textBox1)
    Me.Controls.Add(Me.dataGridView1)
    
    ' Declare the DataSet and add a table and column.
    Dim set1 As New DataSet()
    set1.Tables.Add("Menu")
    set1.Tables(0).Columns.Add("Beverages")
    
    ' Add some rows to the table.
    set1.Tables(0).Rows.Add("coffee")
    set1.Tables(0).Rows.Add("tea")
    set1.Tables(0).Rows.Add("hot chocolate")
    set1.Tables(0).Rows.Add("milk")
    set1.Tables(0).Rows.Add("orange juice")

    ' Add the control data bindings.
    dataGridView1.DataSource = set1
    dataGridView1.DataMember = "Menu"
    textBox1.DataBindings.Add("Text", set1, "Menu.Beverages", _
        True, DataSourceUpdateMode.OnPropertyChanged)
    textBox2.DataBindings.Add("Text", set1, "Menu.Beverages", _
        True, DataSourceUpdateMode.OnPropertyChanged)

    ' Get the BindingManagerBase for this binding.
    bmb = Me.BindingContext(set1, "Menu")

End Sub

Private Sub bmb_BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs) _
    Handles bmb.BindingComplete

    ' Check if the data source has been updated, and that no error has occurred.
    If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate _
        AndAlso e.Exception Is Nothing Then

        ' If not, end the current edit.
        e.Binding.BindingManagerBase.EndCurrentEdit()
    End If
End Sub

注釈

データ ソースに複数のオブジェクトが含まれる一連のBindingManagerBase オブジェクトをBindingが管理する場合は、このオーバーロードを使用します。 たとえば、DataSetには、DataTable オブジェクトによってリンクされた複数のDataRelation オブジェクトを含めることができます。 このような場合、 BindingContext が正しい BindingManagerBaseを返せるようにするには、ナビゲーション パスが必要です。

Note

Item[] パラメーターが有効な場合、BindingManagerBase プロパティは常にdataMemberを返します。 nullは返されません。

使用可能なデータ ソースの一覧と、コントロールとデータ ソース間のバインドの作成については、 Binding クラスを参照してください。

目的の BindingManagerBase がリストを管理する場合は、ナビゲーション パスもリストで終わる必要があります。 たとえば、次の C# コードでは、 TextBox コントロールを注文テーブルの注文日にバインドします。 ナビゲーション パスには、 TableName、 RelationName、および ColumnNameが含まれます。 ただし、 BindingManagerBase は、 TableName と RelationName (リストに解決される) のみを使用して取得する必要があります。

// The navigation path for a Binding ends with a property.
textBox1.DataBindings.Add
("Text", dataSet1, "Customers.custToOrders.OrderDate");
// The navigation path for the BindingManagerBase ends with a list.
BindingManagerBase bmOrders = this.BindingContext
[dataSet1, "Customers.custToOrders"];

BindingManagerBaseを返すときは、Bindingと同じデータ ソースを使用し、ナビゲーション パスのみを変更する必要があります。

Containsメソッドを使用して、目的のBindingManagerBaseが既に存在するかどうかを判断します。

こちらもご覧ください

適用対象

Item[Object]

ソース:
BindingContext.cs
ソース:
BindingContext.cs
ソース:
BindingContext.cs
ソース:
BindingContext.cs
ソース:
BindingContext.cs

指定したデータ ソースに関連付けられている BindingManagerBase を取得します。

public:
 property System::Windows::Forms::BindingManagerBase ^ default[System::Object ^] { System::Windows::Forms::BindingManagerBase ^ get(System::Object ^ dataSource); };
public System.Windows.Forms.BindingManagerBase this[object dataSource] { get; }
member this.Item(obj) : System.Windows.Forms.BindingManagerBase
Default Public ReadOnly Property Item(dataSource As Object) As BindingManagerBase

パラメーター

dataSource
Object

特定の BindingManagerBaseに関連付けられているデータ ソース。

プロパティ値

指定したデータ ソースの BindingManagerBase 。

例

次のコード例は、3 つのBindingManagerBase オブジェクトを返します。1 つはDataView用、1 つはArrayList用、1 つはDataSource コントロールに属するBindingのTextBoxです。

void ReturnBindingManagerBase()
{
   
   // Get the BindingManagerBase for a DataView. 
   BindingManagerBase^ bmCustomers = this->BindingContext[ myDataView ];
   
   /* Get the BindingManagerBase for an ArrayList. */
   BindingManagerBase^ bmOrders = this->BindingContext[ myArrayList ];
   
   // Get the BindingManagerBase for a TextBox control.
   BindingManagerBase^ baseArray = this->BindingContext[ textBox1->DataBindings[ nullptr ]->DataSource ];
}
private void ReturnBindingManagerBase()
{
   // Get the BindingManagerBase for a DataView. 
   BindingManagerBase bmCustomers = 
   this.BindingContext [myDataView];

   /* Get the BindingManagerBase for an ArrayList. */ 
   BindingManagerBase bmOrders = 
   this.BindingContext[myArrayList];

   // Get the BindingManagerBase for a TextBox control.
   BindingManagerBase baseArray = 
   this.BindingContext[textBox1.DataBindings[0].DataSource];
}
Private Sub ReturnBindingManagerBase()
   ' Get the BindingManagerBase for a DataView. 
   Dim bmCustomers As BindingManagerBase = _
      Me.BindingContext(myDataView)

   ' Get the BindingManagerBase for an ArrayList.
   Dim bmOrders As BindingManagerBase = _
      Me.BindingContext(myArrayList)

   ' Get the BindingManagerBase for a TextBox control.
   Dim baseArray As BindingManagerBase = _
      Me.BindingContext(Text1.DataBindings(0).DataSource)
End Sub

注釈

必要な BindingManagerBase にナビゲーション パスが不要な場合は、このオーバーロードを使用します。 たとえば、BindingManagerBaseとしてBindingまたはArrayListを使用する一連のDataTable オブジェクトをDataSourceが管理する場合、ナビゲーション パスは必要ありません。

Note

Item[] プロパティは常にBindingManagerBaseを返し、nullは返しません。

使用可能なデータ ソースの一覧と、コントロールとデータ ソース間のバインドの作成については、 Binding クラスを参照してください。

こちらもご覧ください

適用対象