Files
docs-desktop/dotnet-desktop-guide/framework/winforms/change-notification-in-windows-forms-data-binding.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* 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
2020-09-01 16:26:21 -07:00

4.5 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
Change Notification in Data Binding 03/30/2017
Windows Forms, data binding
Windows Forms, adding change notification for data binding
b5b10f90-0585-41d9-a377-409835262a92

Change Notification in Windows Forms Data Binding

One of the most important concepts of Windows Forms data binding is change notification. To ensure that your data source and bound controls always have the most recent data, you must add change notification for data binding. Specifically, you want to ensure that bound controls are notified of changes that were made to their data source, and the data source is notified of changes that were made to the bound properties of a control.

There are different kinds of change notification, depending on the kind of data binding:

  • Simple binding, in which a single control property is bound to a single instance of an object.

  • List-based binding, which can include a single control property bound to the property of an item in a list or a control property bound to a list of objects.

Additionally, if you are creating Windows Forms controls that you want to use for data binding, you must apply the PropertyNameChanged pattern to the controls, so that changes to the bound property of a control are propagated to the data source.

Change Notification for Simple Binding

For simple binding, business objects must provide change notification when the value of a bound property changes. You can do this by exposing an PropertyNameChanged event for each property of your business object and binding the business object to controls with the xref:System.Windows.Forms.BindingSource or the preferred method in which your business object implements the xref:System.ComponentModel.INotifyPropertyChanged interface and raises a xref:System.ComponentModel.INotifyPropertyChanged.PropertyChanged event when the value of a property changes. For more information, see How to: Implement the INotifyPropertyChanged Interface. When you use objects that implement the xref:System.ComponentModel.INotifyPropertyChanged interface, you do not have to use the xref:System.Windows.Forms.BindingSource to bind the object to a control, but using the xref:System.Windows.Forms.BindingSource is recommended.

Change Notification for List-Based Binding

Windows Forms depends on a bound list to provide property change (a list item property value changes) and list changed (an item is deleted or added to the list) information to bound controls. Therefore, lists used for data binding must implement the xref:System.ComponentModel.IBindingList, which provides both types of change notification. The xref:System.ComponentModel.BindingList%601 is a generic implementation of xref:System.ComponentModel.IBindingList and is designed for use with Windows Forms data binding. You can create a xref:System.ComponentModel.BindingList%601 that contains a business object type that implements xref:System.ComponentModel.INotifyPropertyChanged and the list will automatically convert the xref:System.ComponentModel.INotifyPropertyChanged.PropertyChanged events to xref:System.ComponentModel.IBindingList.ListChanged events. If the bound list is not an xref:System.ComponentModel.IBindingList, you must bind the list of objects to Windows Forms controls by using the xref:System.Windows.Forms.BindingSource component. The xref:System.Windows.Forms.BindingSource component will provide property-to-list conversion similar to that of the xref:System.ComponentModel.BindingList%601. For more information, see How to: Raise Change Notifications Using a BindingSource and the INotifyPropertyChanged Interface.

Change Notification for Custom Controls

Finally, from the control side you must expose a PropertyNameChanged event for each property designed to be bound to data. The changes to the control property are then propagated to the bound data source. For more information, see How to: Apply the PropertyNameChanged Pattern

See also