次の方法で共有


方法 : 複数のコントロールを 1 つのデータ ソースにバインドして同期状態を保つ

Windows フォームでデータ バインディングを操作するときには、複数のコントロールを 1 つのデータ ソースにバインドすることがよくあります。 場合によっては、コントロールのバインド プロパティを他のコントロールやデータ ソースと同期した状態を保つために、追加の手順が必要になります。 追加の手順が必要となるケースとして、次の 2 つが挙げられます。

前者の場合、BindingSource を使用してコントロールにデータ ソースをバインドできます。 後者の場合、BindingSource を使用して BindingComplete イベントを処理し、バインドされた BindingManagerBaseEndCurrentEdit を呼び出します。

使用例

次のコード例は、3 つのコントロール (2 つのテキスト ボックスと 1 つの DataGridView コントロール) について、BindingSource コンポーネントを使用して DataSet の同じ列にバインドする方法を示します。 この例は、BindingComplete イベントを処理して、1 つのテキスト ボックスのテキスト値が変更されると、その他のテキスト ボックスと DataGridView コントロールを適切な値で更新する方法を示しています。

この例では、BindingSource を使用してデータ ソースとコントロールをバインドします。 または、コントロールをデータ ソースに直接バインドし、フォームの BindingContext からバインディングの BindingManagerBase を取得した後、BindingManagerBaseBindingComplete イベントを処理することもできます。 この処理方法の例については、BindingManagerBaseBindingComplete イベントに関するヘルプ ページを参照してください。

' Declare the controls to be used.
Private WithEvents bindingSource1 As BindingSource
Private WithEvents textBox1 As TextBox
Private WithEvents textBox2 As TextBox
Private WithEvents dataGridView1 As DataGridView


Private Sub InitializeControlsAndDataSource() 
    ' Initialize the controls and set location, size and 
    ' other basic properties.
    Me.dataGridView1 = New DataGridView()
    Me.bindingSource1 = New BindingSource()
    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")

    ' 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 'InitializeControlsAndDataSource

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 occured.
    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

        // Declare the controls to be used.
        private BindingSource bindingSource1;
        private TextBox textBox1;
        private TextBox textBox2;
        private DataGridView dataGridView1;

        private void InitializeControlsAndDataSource()
        {
            // Initialize the controls and set location, size and 
            // other basic properties.
            this.dataGridView1 = new DataGridView();
            this.bindingSource1 = new BindingSource();
            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");

            // 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);
        }

        private void bindingSource1_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();
        }

コードのコンパイル

  • このコード例で必要な要件は次のとおりです。

  • System アセンブリ、System.Windows.Forms アセンブリ、および System.Drawing アセンブリへの参照。

  • Load イベントを処理するフォーム。このフォームの Load イベント ハンドラーから、コード例の InitializeControlsAndDataSource メソッドを呼び出す必要があります。

参照

処理手順

方法 : BindingSource コンポーネントを使用してフォーム間でバインド データを共有する

概念

Windows フォーム データ バインディングの変更通知

データ連結に関連するインターフェイス

その他の技術情報

Windows フォームでのデータ バインディング