BindingContext.Item[] Proprietà

Definizione

Ottiene un oggetto BindingManagerBase.

Overload

Nome Descrizione
Item[Object, String]

Ottiene un BindingManagerBase oggetto associato all'origine dati e al membro dati specificato.

Item[Object]

Ottiene l'oggetto BindingManagerBase associato all'origine dati specificata.

Item[Object, String]

Ottiene un BindingManagerBase oggetto associato all'origine dati e al membro dati specificato.

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

Parametri

dataSource
Object

Origine dati associata a un particolare BindingManagerBaseoggetto .

dataMember
String

Percorso di spostamento contenente le informazioni che si risolve in un oggetto specifico BindingManagerBase.

Valore della proprietà

Oggetto BindingManagerBase per l'origine dati e il membro dati specificati.

Eccezioni

L'oggetto specificato dataMember non esiste all'interno dell'origine dati.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare per recuperare l'oggetto Item[]BindingManagerBase per una determinata associazione. Viene inoltre illustrato come gestire l'evento BindingComplete per BindingManagerBase garantire che più controlli associati alla stessa origine dati rimangano sincronizzati quando uno dei valori del controllo viene modificato. Per eseguire questo esempio, incollare il codice in un Windows Form e chiamare il InitializeControlsAndData metodo dal costruttore o Load dal metodo di gestione degli eventi del modulo.

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

Commenti

Utilizzare questo overload quando gestisce BindingManagerBase un set di Binding oggetti per cui l'origine dati contiene più oggetti. Ad esempio, un DataSet oggetto può contenere diversi DataTable oggetti collegati da DataRelation oggetti . In tal caso, il percorso di spostamento è necessario per consentire all'oggetto BindingContext di restituire l'oggetto corretto BindingManagerBase.

Note

La Item[] proprietà restituirà sempre un BindingManagerBaseoggetto , quando il dataMember parametro è valido. Non restituirà nullmai .

Vedere la Binding classe per un elenco delle possibili origini dati e per informazioni sulla creazione di associazioni tra controlli e origini dati.

Se l'oggetto desiderato BindingManagerBase gestisce un elenco, il percorso di spostamento deve terminare anche con un elenco. Ad esempio, il codice C# seguente associa un TextBox controllo alla data dell'ordine in una tabella di ordini. Il percorso di spostamento include , TableNameRelationNamee .ColumnName Tuttavia, deve BindingManagerBase essere recuperato solo usando e TableNameRelationName (che si risolve in un elenco).

// 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 un oggetto BindingManagerBase, è consigliabile usare la stessa origine dati dell'oggetto Binding e modificare solo il percorso di spostamento.

Utilizzare il Contains metodo per determinare se l'oggetto desiderato BindingManagerBase esiste già.

Vedi anche

Si applica a

Item[Object]

Ottiene l'oggetto BindingManagerBase associato all'origine dati specificata.

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

Parametri

dataSource
Object

Origine dati associata a un particolare BindingManagerBaseoggetto .

Valore della proprietà

Oggetto BindingManagerBase per l'origine dati specificata.

Esempio

Nell'esempio di codice seguente vengono restituiti tre BindingManagerBase oggetti: uno per , DataViewuno per un ArrayListoggetto e uno per DataSource di un Binding oggetto che appartiene a un TextBox controllo .

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

Commenti

Usare questo overload se l'oggetto BindingManagerBase desiderato non richiede un percorso di spostamento. Ad esempio, se gestisce BindingManagerBase un set di Binding oggetti che usano un ArrayList oggetto o DataTable come DataSource, non è necessario alcun percorso di spostamento.

Note

La Item[] proprietà restituirà sempre un BindingManagerBaseoggetto e non restituirà nullmai .

Vedere la Binding classe per un elenco delle possibili origini dati e per informazioni sulla creazione di associazioni tra controlli e origini dati.

Vedi anche

Si applica a