* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
3.8 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|
| How to: Ensure Multiple Controls Bound to the Same Data Source Remain Synchronized | 03/30/2017 |
|
|
c2f0ecc6-11e6-4c2c-a1ca-0759630c451e |
How to: Ensure Multiple Controls Bound to the Same Data Source Remain Synchronized
Oftentimes when working with data binding in Windows Forms, multiple controls are bound to the same data source. In some cases, it may be necessary to take extra steps to ensure that the bound properties of the controls remain synchronized with each other and the data source. These steps are necessary in two situations:
-
If the data source does not implement xref:System.ComponentModel.IBindingList, and therefore generate xref:System.ComponentModel.IBindingList.ListChanged events of type xref:System.ComponentModel.ListChangedType.ItemChanged.
-
If the data source implements xref:System.ComponentModel.IEditableObject.
In the former case, you can use a xref:System.Windows.Forms.BindingSource to bind the data source to the controls. In the latter case, you use a xref:System.Windows.Forms.BindingSource and handle the xref:System.Windows.Forms.BindingSource.BindingComplete event and call xref:System.Windows.Forms.BindingManagerBase.EndCurrentEdit%2A on the associated xref:System.Windows.Forms.BindingManagerBase.
Example
The following code example demonstrates how to bind three controls—two text-box controls and a xref:System.Windows.Forms.DataGridView control—to the same column in a xref:System.Data.DataSet using a xref:System.Windows.Forms.BindingSource component. This example demonstrates how to handle the xref:System.Windows.Forms.BindingSource.BindingComplete event and ensure that when the text value of one text box is changed, the additional text box and the xref:System.Windows.Forms.DataGridView control are updated with the correct value.
The example uses a xref:System.Windows.Forms.BindingSource to bind the data source and the controls. Alternatively, you can bind the controls directly to the data source and retrieve the xref:System.Windows.Forms.BindingManagerBase for the binding from the form's xref:System.Windows.Forms.Control.BindingContext%2A and then handle the xref:System.Windows.Forms.BindingManagerBase.BindingComplete event for the xref:System.Windows.Forms.BindingManagerBase. For an example of how to do this, see the Help page about the xref:System.Windows.Forms.BindingManagerBase.BindingComplete event of xref:System.Windows.Forms.BindingManagerBase.
[!code-csharpSystem.Windows.Forms.BindingSourceMultipleControls#1] [!code-vbSystem.Windows.Forms.BindingSourceMultipleControls#1]
Compiling the Code
-
This code example requires
-
References to the xref:System, xref:System.Windows.Forms, and xref:System.Drawing assemblies.
-
A form with the xref:System.Windows.Forms.Form.Load event handled and a call to the
InitializeControlsAndDataSourcemethod in the example from the form's xref:System.Windows.Forms.Form.Load event handler.