BindingContext.Item[] 属性

定义

获取一个 BindingManagerBase

重载

名称 说明
Item[Object, String]

获取与指定数据源和数据成员关联的项 BindingManagerBase

Item[Object]

BindingManagerBase获取与指定数据源关联的项。

Item[Object, String]

Source:
BindingContext.cs
Source:
BindingContext.cs
Source:
BindingContext.cs
Source:
BindingContext.cs
Source:
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 特定绑定。 它还演示如何处理BindingCompleteBindingManagerBase事件,以确保在更改其中一个控件值时,绑定到同一数据源的多个控件保持同步。 若要运行此示例,请将代码粘贴到 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 对象可以包含多个 DataTableDataRelation 对象链接的对象。 在这种情况下,需要导航路径才能返回 BindingContext 正确的 BindingManagerBase路径。

注释

当参数有效时,该Item[]属性将始终返回一个BindingManagerBasedataMember 它永远不会返回 null

Binding有关可能的数据源的列表以及有关在控件和数据源之间创建绑定的信息,请参阅该类。

BindingManagerBase如果需要管理列表,则导航路径还必须以列表结尾。 例如,以下 C# 代码将 TextBox 控件绑定到订单表中的订单日期。 导航路径包括 TableName、和 RelationNameColumnName。 但是, BindingManagerBase 必须仅使用解析为列表的 TableName and 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"];

返回 a BindingManagerBase时,应使用与导航路径相同的数据源 Binding 并仅修改导航路径。

使用该方法 Contains 确定所需 BindingManagerBase 是否已存在。

另请参阅

适用于

Item[Object]

Source:
BindingContext.cs
Source:
BindingContext.cs
Source:
BindingContext.cs
Source:
BindingContext.cs
Source:
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关联的数据源。

属性值

指定数据源的 A BindingManagerBase

示例

下面的代码示例返回三 BindingManagerBase 个对象:一个 DataView对象,一个用于一 ArrayList个对象,一个用于 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 使用 ArrayListDataTable 用作 DataSource对象的对象,则不需要导航路径。

注释

Item[] 属性将始终返回一个 BindingManagerBase,并且永远不会返回 null

Binding有关可能的数据源的列表以及有关在控件和数据源之间创建绑定的信息,请参阅该类。

另请参阅

适用于