BindingContext.Item[] 属性  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个 BindingManagerBase。
重载
| Item[Object, String] | 获取与指定数据源和数据成员相关联的一个 BindingManagerBase。 | 
| Item[Object] | 获取与指定数据源关联的 BindingManagerBase。 | 
Item[Object, String]
获取与指定数据源和数据成员相关联的一个 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.BindingManagerBaseDefault 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
注解
当 管理数据源包含多个对象的一组Binding对象时BindingManagerBase,请使用此重载。 例如, DataSet 可以包含 DataTable 多个由 DataRelation 对象链接的对象。 在这种情况下,需要导航路径才能使 返回 BindingContext 正确的 BindingManagerBase。
注意
当 Item[] 参数有效时,dataMember属性将始终返回 BindingManagerBase。 它永远不会返回 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]
获取与指定数据源关联的 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.BindingManagerBaseDefault Public ReadOnly Property Item(dataSource As Object) As BindingManagerBase参数
- dataSource
- Object
与特定 BindingManagerBase 关联的数据源。
属性值
指定数据源的 BindingManagerBase。
示例
下面的代码示例返回三BindingManagerBase个 对象:一个DataView用于 ,一个ArrayList用于 , 一个用于 DataSource 属于控件的 。BindingTextBox
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 管理使用 或 DataTable 作为 DataSource的一组Binding对象ArrayList,则不需要导航路径。
注意
属性 Item[] 将始终返回 BindingManagerBase,并且从不返回 null。
Binding有关可能数据源的列表以及有关在控件和数据源之间创建绑定的信息,请参阅 类。