BindingContext.Item[Object] 属性

定义

重载

Item[Object, String]

获取与指定数据源和数据成员相关联的一个 BindingManagerBaseGets a BindingManagerBase that is associated with the specified data source and data member.

Item[Object]

获取与指定数据源关联的 BindingManagerBaseGets the BindingManagerBase that is associated with the specified data source.

Item[Object, String]

获取与指定数据源和数据成员相关联的一个 BindingManagerBaseGets a BindingManagerBase that is associated with the specified data source and data member.

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; }
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 关联的数据源。The data source associated with a particular BindingManagerBase.

dataMember
String

包含解析为特定 BindingManagerBase 的信息的导航路径。A navigation path containing the information that resolves to a specific BindingManagerBase.

属性值

指定数据源和数据成员的 BindingManagerBaseThe BindingManagerBase for the specified data source and data member.

例外

数据源内不存在指定的 dataMemberThe specified dataMember does not exist within the data source.

示例

下面的代码示例演示如何使用 Item[Object] 检索特定绑定的 BindingManagerBaseThe following code example demonstrates how to use the Item[Object] to retrieve the BindingManagerBase for a particular binding. 它还说明了如何处理 BindingManagerBaseBindingComplete 事件,以确保在更改其中一个控件值时,绑定到同一个数据源的多个控件保持同步。It also shows how to handle the BindingComplete event for the BindingManagerBase to ensure multiple controls bound to the same data source remain synchronized when one of the control values is changed. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的构造函数或 Load 事件处理方法调用 InitializeControlsAndData 方法。To run this example, paste the code into a Windows Form and call the InitializeControlsAndData method from the form's constructor or Load event-handling method.

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 对象时,请使用此重载。Use this overload when the BindingManagerBase manages a set of Binding objects for which the data source contains multiple objects. 例如,一个 DataSet 可以包含由 DataRelation 对象链接的多个 DataTable 对象。For example, a DataSet can contain several DataTable objects linked by DataRelation objects. 在这种情况下,导航路径是必需的,以便让 BindingContext 返回正确的 BindingManagerBaseIn such a case, the navigation path is required to enable the BindingContext to return the correct BindingManagerBase.

备注

dataMember 参数有效时,Item[Object] 属性将始终返回 BindingManagerBaseThe Item[Object] property will always return a BindingManagerBase, when the dataMember parameter is valid. 它绝不会返回 nullIt will never return null.

有关可能的数据源的列表,请参阅 Binding 类,有关在控件和数据源之间创建绑定的信息,请参阅。See the Binding class for a list of possible data sources and for information about creating bindings between controls and data sources.

如果所需 BindingManagerBase 管理列表,则导航路径也必须以列表结尾。If the desired BindingManagerBase manages a list, the navigation path must also end with a list. 例如,以下C#代码将 TextBox 控件绑定到订单表中的订单日期。For example, the following C# code binds a TextBox control to the order date in a table of orders. 导航路径包括 TableNameRelationNameColumnNameThe navigation path includes the TableName, the RelationName, and the ColumnName. 但是,只能使用 TableNameRelationName (解析为列表)检索 BindingManagerBaseHowever, the BindingManagerBase must be retrieved using only the TableName and RelationName (which resolves to a list).

// 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 相同的数据源,并仅修改导航路径。When returning a BindingManagerBase, you should use the same data source as the Binding and modify only the navigation path.

使用 Contains 方法来确定所需的 BindingManagerBase 是否已存在。Use the Contains method to determine if the desired BindingManagerBase already exists.

另请参阅

Item[Object]

获取与指定数据源关联的 BindingManagerBaseGets the BindingManagerBase that is associated with the specified data source.

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 关联的数据源。The data source associated with a particular BindingManagerBase.

属性值

指定数据源的 BindingManagerBaseA BindingManagerBase for the specified data source.

示例

下面的代码示例返回三个 BindingManagerBase 对象:一个用于 DataView,一个用于 ArrayList,另一个用于属于 Binding 控件的 TextBoxDataSource 中。The following code example returns three BindingManagerBase objects: one for a DataView, one for an ArrayList, and one for the DataSource of a Binding that belongs to a TextBox control.

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 不需要导航路径,请使用此重载。Use this overload if the BindingManagerBase you want does not require a navigation path. 例如,如果 BindingManagerBase 管理一组使用 ArrayListDataTable 作为 DataSourceBinding 对象,则无需导航路径。For example, if the BindingManagerBase manages a set of Binding objects that use an ArrayList or DataTable as the DataSource, no navigation path is required.

备注

Item[Object] 属性将始终返回 BindingManagerBase,并且绝不会返回 nullThe Item[Object] property will always return a BindingManagerBase, and never return null.

有关可能的数据源的列表,请参阅 Binding 类,有关在控件和数据源之间创建绑定的信息,请参阅。See the Binding class for a list of possible data sources and for information about creating bindings between controls and data sources.

另请参阅

适用于