Condividi tramite


Sincronizzare più controlli con la stessa origine dati (Windows Form .NET)

Durante l'implementazione del data binding in Windows Form, più controlli sono associati alla stessa origine dati. Nelle situazioni seguenti è necessario assicurarsi che le proprietà associate del controllo rimangano sincronizzate tra loro e con l'origine dati:

Nel caso precedente, è possibile usare un BindingSource oggetto per associare l'origine dati ai controlli. In quest'ultimo caso, si usa un e BindingSource si gestisce l'evento BindingComplete e si chiama EndCurrentEdit sull'oggetto associato BindingManagerBase.

Esempio di associazione di controlli tramite BindingSource

Nell'esempio di codice seguente viene illustrato come associare tre controlli, due controlli casella di testo e un DataGridView controllo alla stessa colonna in un DataSet oggetto utilizzando un BindingSource componente. Nell'esempio viene illustrato come gestire l'evento BindingComplete . Garantisce che quando viene modificato il valore di testo di una casella di testo, l'altra casella di testo e il DataGridView controllo vengono aggiornati con il valore corretto.

Nell'esempio viene utilizzato un BindingSource oggetto per associare l'origine dati e i controlli . In alternativa, è possibile associare i controlli direttamente all'origine dati e recuperare l'oggetto BindingManagerBase per l'associazione dal form BindingContext e quindi gestire l'evento BindingComplete per .BindingManagerBase Per altre informazioni sull'associazione dell'origine dati e dei controlli, vedere la pagina della Guida sull'evento BindingComplete di BindingManagerBase.

public Form1() 
{
    InitializeComponent();
    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");

    // Set the data source to the DataSet.
    bindingSource1.DataSource = set1;

    //Set the DataMember to the Menu table.
    bindingSource1.DataMember = "Menu";

    // Add the control data bindings.
    dataGridView1.DataSource = bindingSource1;
    textBox1.DataBindings.Add("Text", bindingSource1,
        "Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
    textBox2.DataBindings.Add("Text", bindingSource1,
        "Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
    bindingSource1.BindingComplete +=
        new BindingCompleteEventHandler(bindingSource1_BindingComplete);
}

void bindingSource1_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();
}
Public Class Form1
    Private Sub InitializeControlsAndDataSource()
        ' Add a table and column to 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")

        ' Set the data source to the DataSet.
        BindingSource1.DataSource = set1

        'Set the DataMember to the Menu table.
        BindingSource1.DataMember = "Menu"

        ' Add the control data bindings.
        DataGridView1.DataSource = BindingSource1
        TextBox1.DataBindings.Add("Text", BindingSource1, "Beverages",
            True, DataSourceUpdateMode.OnPropertyChanged)
        TextBox2.DataBindings.Add("Text", BindingSource1, "Beverages",
            True, DataSourceUpdateMode.OnPropertyChanged)
    End Sub

    Private Sub BindingSource1_BindingComplete(ByVal sender As Object,
        ByVal e As BindingCompleteEventArgs) Handles BindingSource1.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
        
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        InitializeControlsAndDataSource()
    End Sub
End Class

Vedi anche