BindingContext.Item[] Property

Definition

Ottiene un oggetto BindingManagerBase.Gets a BindingManagerBase.

Overloads

Item[Object, String]

Ottiene l'oggetto BindingManagerBase associato all'origine e al membro dati specificati.Gets a BindingManagerBase that is associated with the specified data source and data member.

Item[Object]

Ottiene l'oggetto BindingManagerBase associato all'origine dati specificata.Gets the BindingManagerBase that is associated with the specified data source.

Item[Object, String]

Ottiene l'oggetto BindingManagerBase associato all'origine e al membro dati specificati.Gets 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

Parameters

dataSource
Object

Origine dati associata a un particolare oggetto BindingManagerBase.The data source associated with a particular BindingManagerBase.

dataMember
String

Percorso di navigazione contenente le informazioni che vengono risolte in uno specifico oggetto BindingManagerBase.A navigation path containing the information that resolves to a specific BindingManagerBase.

Property Value

BindingManagerBase

Oggetto BindingManagerBase per l'origine e il membro dati specificati.The BindingManagerBase for the specified data source and data member.

Exceptions

Il parametro dataMember specificato non esiste nell'origine dati.The specified dataMember does not exist within the data source.

Examples

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare il Item[] per recuperare il BindingManagerBase per una determinata associazione.The following code example demonstrates how to use the Item[] to retrieve the BindingManagerBase for a particular binding. Viene inoltre illustrato come gestire l'evento BindingComplete per la BindingManagerBase per assicurarsi che più controlli associati alla stessa origine dati rimangano sincronizzati quando uno dei valori del controllo viene modificato.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. Per eseguire questo esempio, incollare il codice in un Windows Form e chiamare il metodo InitializeControlsAndData dal costruttore del form o Load metodo di gestione degli eventi.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

Remarks

Utilizzare questo overload quando il BindingManagerBase gestisce un set di Binding oggetti per i quali l'origine dati contiene più oggetti.Use this overload when the BindingManagerBase manages a set of Binding objects for which the data source contains multiple objects. Un DataSet, ad esempio, può contenere diversi DataTable oggetti collegati da DataRelation oggetti.For example, a DataSet can contain several DataTable objects linked by DataRelation objects. In tal caso, il percorso di navigazione è necessario per consentire al BindingContext di restituire la BindingManagerBasecorretta.In such a case, the navigation path is required to enable the BindingContext to return the correct BindingManagerBase.

Note

La proprietà Item[] restituirà sempre un BindingManagerBase, quando il parametro dataMember è valido.The Item[] property will always return a BindingManagerBase, when the dataMember parameter is valid. Non verrà mai restituito null.It will never return null.

Vedere la classe Binding per un elenco di possibili origini dati e per informazioni sulla creazione di associazioni tra controlli e origini dati.See the Binding class for a list of possible data sources and for information about creating bindings between controls and data sources.

Se la BindingManagerBase desiderata gestisce un elenco, anche il percorso di navigazione deve terminare con un elenco.If the desired BindingManagerBase manages a list, the navigation path must also end with a list. Il codice seguente C# , ad esempio, associa un controllo TextBox alla data dell'ordine in una tabella di ordini.For example, the following C# code binds a TextBox control to the order date in a table of orders. Il percorso di navigazione include il TableName, il RelationNamee il ColumnName.The navigation path includes the TableName, the RelationName, and the ColumnName. Tuttavia, il BindingManagerBase deve essere recuperato utilizzando solo i TableName e RelationName (che viene risolto in un elenco).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"];  

Quando si restituisce una BindingManagerBase, è necessario usare la stessa origine dati del Binding e modificare solo il percorso di navigazione.When returning a BindingManagerBase, you should use the same data source as the Binding and modify only the navigation path.

Utilizzare il metodo Contains per determinare se il BindingManagerBase desiderato esiste già.Use the Contains method to determine if the desired BindingManagerBase already exists.

See also

Item[Object]

Ottiene l'oggetto BindingManagerBase associato all'origine dati specificata.Gets 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

Parameters

dataSource
Object

Origine dati associata a un particolare oggetto BindingManagerBase.The data source associated with a particular BindingManagerBase.

Property Value

BindingManagerBase

Oggetto BindingManagerBase per l'origine dati specificata.A BindingManagerBase for the specified data source.

Examples

Nell'esempio di codice seguente vengono restituiti tre oggetti BindingManagerBase: uno per un DataView, uno per un ArrayListe uno per la DataSource di un Binding che appartiene a un controllo TextBox.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

Remarks

Utilizzare questo overload se il BindingManagerBase desiderato non richiede un percorso di navigazione.Use this overload if the BindingManagerBase you want does not require a navigation path. Se, ad esempio, il BindingManagerBase gestisce un set di Binding oggetti che utilizzano un ArrayList o DataTable come DataSource, non è necessario alcun percorso di navigazione.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.

Note

La proprietà Item[] restituirà sempre un BindingManagerBasee non restituirà mai null.The Item[] property will always return a BindingManagerBase, and never return null.

Vedere la classe Binding per un elenco di possibili origini dati e per informazioni sulla creazione di associazioni tra controlli e origini dati.See the Binding class for a list of possible data sources and for information about creating bindings between controls and data sources.

See also

Applies to