Export (0) Print
Expand All

BindingContext.Item Property (Object, String)

Gets a BindingManagerBase that is associated with the specified data source and data member.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public BindingManagerBase this [
	Object dataSource,
	string dataMember
] { get; }
/** @property */
public BindingManagerBase get_Item (Object dataSource, String dataMember)

Not applicable.

Parameters

dataSource

The data source associated with a particular BindingManagerBase.

dataMember

A navigation path containing the information that resolves to a specific BindingManagerBase.

Property Value

The BindingManagerBase for the specified data source and data member.

Exception typeCondition

Exception

The specified dataMember does not exist within the data source.

Use this overload when the BindingManagerBase manages a set of Binding objects for which the data source contains multiple objects. For example, a DataSet can contain several DataTable objects linked by DataRelation objects. In such a case, the navigation path is required to enable the BindingContext to return the correct BindingManagerBase.

NoteNote:

The Item property will always return a BindingManagerBase, when the dataMember parameter is valid. It will never return a null reference (Nothing in Visual Basic).

See the Binding class for a list of possible data sources and for information about creating bindings between controls and data sources.

If the desired BindingManagerBase manages a list, the navigation path must also end with a list. For example, the following C# code binds a TextBox control to the order date in a table of orders. The navigation path includes the TableName, the RelationName, and the ColumnName. However, 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"];

When returning a BindingManagerBase, you should use the same data source as the Binding and modify only the navigation path.

Use the Contains method to determine if the desired BindingManagerBase already exists.

The following code example demonstrates how to use the Item to retrieve the BindingManagerBase for a particular binding. 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. 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 occured.
    if (e.BindingCompleteContext ==
        BindingCompleteContext.DataSourceUpdate && e.Exception == null)

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft