--- title: "BindingSource Component Architecture" ms.date: "03/30/2017" helpviewer_keywords: - "BindingSource component [Windows Forms], architecture" - "Windows Forms, data binding" - "BindingSource component [Windows Forms], about BindingSource component" - "data binding [Windows Forms], BindingSource component" ms.assetid: 7bc69c90-8a11-48b1-9336-3adab5b41591 --- # BindingSource Component Architecture With the component, you can universally bind all Windows Forms controls to data sources. The component simplifies the process of binding controls to a data source and provides the following advantages over traditional data binding: - Enables design-time binding to business objects. - Encapsulates functionality and exposes events at design time. - Simplifies creating a list that supports the interface by providing list change notification for data sources that do not natively support list change notification. - Provides an extensibility point for the method. - Provides a level of indirection between the data source and the control. This indirection is important when the data source may change at run time. - Interoperates with other data-related Windows Forms controls, specifically the and the controls. For these reasons, the component is the preferred way to bind your Windows Forms controls to data sources. ## BindingSource Features The component provides several features for binding controls to data. With these features, you can implement most data-binding scenarios with almost no coding on your part. The component accomplishes this by providing a consistent interface for accessing many different kinds of data sources. This means that you use the same procedure for binding to any type. For example, you can attach the property to a or to a business object and in both cases you use the same set of properties, methods, and events to manipulate the data source. The consistent interface provided by the component greatly simplifies the process of binding data to controls. For data-source types that provide change notification, the component automatically communicates changes between the control and the data source. For data-source types that do not provide change notification, events are provided that let you raise change notifications. The following list shows the features supported by the component: - Indirection. - Currency management. - Data source as a list. - as an . - Custom item creation. - Transactional item creation. - support. - Design-time support. - Static methods. - Sorting and filtering with the interface. - Integration with . ### Indirection The component provides a level of indirection between a control and a data source. Instead of binding a control directly to a data source, you bind the control to a , and you attach the data source to the component's property. With this level of indirection, you can change the data source without resetting the control binding. This gives you the following capabilities: - You can attach the to different data sources while retaining the current control bindings. - You can change items in the data source and notify bound controls. For more information, see [How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource](reflect-data-source-updates-in-a-wf-control-with-the-bindingsource.md). - You can bind to a instead of an object in memory. For more information, see [How to: Bind a Windows Forms Control to a Type](how-to-bind-a-windows-forms-control-to-a-type.md). You can then bind to an object at run time. ### Currency Management The component implements the interface to handle currency management for you. With the interface, you can also access to the currency manager for a , in addition to the currency manager for another bound to the same . The component encapsulates functionality and exposes the most common properties and events. The following table describes some of the members related to currency management. property Gets the currency manager associated with the . method If there is another bound to the specified data member, gets its currency manager. property Gets the current item of the data source. property Gets or sets the current position in the underlying list. method Applies pending changes to the underlying data source. method Cancels the current edit operation. ### Data Source as a List The component implements the and interfaces. With this implementation, you can use the component itself as a data source, without any external storage. When the component is attached to a data source, it exposes the data source as a list. The property can be set to several data sources. These include types, objects, and lists of types. The resulting data source will be exposed as a list. The following table shows some of the common data sources and the resulting list evaluation. |DataSource property|List results| |-------------------------|------------------| |A null reference (`Nothing` in Visual Basic)|An empty of objects. Adding an item sets the list to the type of the added item.| |A null reference (`Nothing` in Visual Basic) with set|Not supported; raises .| |Non-list type or object of type "T"|An empty of type "T".| |Array instance|An containing the array elements.| | instance|An containing the items| |List instance containing type "T"|An instance containing type "T".| Additionally, can be set to other list types, such as and , and the will handle them appropriately. In this case, the type that is contained in the list should have a parameterless constructor. ### BindingSource as an IBindingList The component provides members for accessing and manipulating the underlying data as an . The following table describes some of these members. |Member|Description| |------------|-----------------| | property|Gets the list that results from the evaluation of the or properties.| | method|Adds a new item to the underlying list. Applies to data sources that implement the interface and allow adding items (that is, the property is set to `true`).| ### Custom Item Creation You can handle the event to provide your own item-creation logic. The event occurs before a new object is added to the . This event is raised after the method is called, but before the new item is added to the underlying list. By handling this event, you can provide custom item creation behavior without deriving from the class. For more information, see [How to: Customize Item Addition with the Windows Forms BindingSource](how-to-customize-item-addition-with-the-windows-forms-bindingsource.md). ### Transactional Item Creation The component implements the interface, which enables transactional item creation. After a new item is provisionally created by using a call to , the addition may be committed or rolled back in the following ways: - The method will explicitly commit the pending addition. - Performing another collection operation, such as an insertion, removal, or move, will implicitly commit the pending addition. - The method will roll back the pending addition if the method has not already been committed. ### IEnumerable Support The component enables binding controls to data sources. With this component, you can bind to a data source such as a . When an data source is assigned to the component, the creates an and adds the contents of the data source to the list. ### Design-Time Support Some object types cannot be created at design time, such as objects created from a factory class, or objects returned by a Web service. You may sometimes have to bind your controls to these types at design time, even though there is no object in memory to which your controls can bind. You may, for example, need to label the column headers of a control with the names of your custom type's public properties. To support this scenario, the component supports binding to a . When you assign a to the property, the component creates an empty of items. Any controls you subsequently bind to the component will be alerted to the presence of the properties or schema of your type at design time, or at run time. For more information, see [How to: Bind a Windows Forms Control to a Type](how-to-bind-a-windows-forms-control-to-a-type.md). ### Static ListBindingHelper Methods The , , and types all share common logic to generate a list from a `DataSource`/`DataMember` pair. Additionally, this common logic is publicly exposed for use by control authors and other third parties in the following `static` methods: - - . - - ### Sorting and Filtering with the IBindingListView Interface The component implements the interface, which extends the interface. The offers single column sorting and the offers advanced sorting and filtering. With , you can sort and filter items in the data source, if the data source also implements one of these interfaces. The component does not provide a reference implementation of these members. Instead, calls are forwarded to the underlying list. The following table describes the properties you use for sorting and filtering. |Member|Description| |------------|-----------------| | property|If the data source is an , gets or sets the expression used to filter which rows are viewed.| | property|If the data source is an , gets or sets a column name used for sorting and sort order information.

-or-

If the data source is an and supports advanced sorting, gets multiple column names used for sorting and sort order| ### Integration with BindingNavigator You can use the component to bind any Windows Forms control to a data source, but the control is designed specifically to work with the component. The control provides a user interface for controlling the component's current item. By default, the control provides buttons that correspond to the navigation methods on the component. For more information, see [How to: Navigate Data with the Windows Forms BindingNavigator Control](how-to-navigate-data-with-the-windows-forms-bindingnavigator-control.md). ## See also - - - [BindingSource Component Overview](bindingsource-component-overview.md) - [BindingNavigator Control](bindingnavigator-control-windows-forms.md) - [Windows Forms Data Binding](../windows-forms-data-binding.md) - [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md) - [How to: Bind a Windows Forms Control to a Type](how-to-bind-a-windows-forms-control-to-a-type.md) - [How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource](reflect-data-source-updates-in-a-wf-control-with-the-bindingsource.md)