Introducing the BindingSource


Introducing the BindingSource

To put it bluntly, the BindingSource component is your new best friend. In previous versions of .NET, one of the most frustrating things about data binding is that you could only do so much at design time. You had to wait until your application was running to see the real binding behavior. With the use of the BindingSource component, you can accurately model the binding behavior of all your controls at design time, even if you are binding to an object, a typed DataSet, an XML file, or even a Web Service.

Think of BindingSource as an intermediary or a connector between data bound controls and a data source. With the BindingSource in the middle, there no longer needs to be a tight coupling between the control and the data source, so we can bind controls to virtually any kind of data at design time. In addition to providing an encapsulation around a data source, it also provides standardized methods for starting, committing, and canceling edit operations.

Tables 37.1, 37.2, and 37.3 show some of the commonly used methods, properties, and events of the BindingSource class. It is important to get a good grasp of how this class works before continuing through the rest of this chapter because the BindingSource is used by many of the other components and classes discussed in this chapter.

Table 37.1. Commonly Used BindingSource Methods

Method

Description

Add

Adds an existing item to the underlying list

AddNew

Creates a new item and adds it to the underlying list

CancelEdit

Cancels the current editing operation

CancelNew

Disposes of a new item that hasn't yet been committed

Clear

Removes all items in the underlying list

Contains

Used to determine whether a given item exists in the associated data source

EndEdit

Commits pending changes to the data source

IndexOf

Returns the index of a specified item

Insert

Inserts an item into the list at a given position or index

MoveFirst

Sets the current position of the BindingSource to the beginning of the underlying list

MoveLast

Moves to the last item in the list

MoveNext

Moves to the next item

MovePrevious

Moves to the previous item

Remove

Removes an item from the list

RemoveAt

Removes an item from the list based on its position within the list

RemoveCurrent

Removes the current item from the list

ResetBindings

Tells all controls bound to this binding source to refresh their values

ResetCurrentItem

Refreshes all data-bound values of the current item

ResetItem

Tells all controls bound to the source to refresh their values associated with a given item

ResumeBinding

Resumes data binding

SuspendBinding

Suspends data binding; especially useful during form initialization routines


Table 37.2. Commonly Used BindingSource Properties

Property

Description

AllowNew

Indicates whether or not the BindingSource will allow new items to be created in the list. If AllowNew is true, the AddNew method can be used to create new items.

Count

Gets the number of items in the list.

Current

Gets the current item in the list.

DataMember

Gets or sets the name of the list within the DataSource to which the BindingSource is bound.

DataSource

Works like a .NET 1.1 DataSource propertyindicates the source of data to which the BindingSource is bound.

Filter

Gets or sets a filter expression used to filter items in the list.

Item

Gets or sets the item at a specific index. C# can use array [] notation in place of this property.

List

Gets or sets the list to which the BindingSource is bound.

Position

Gets or sets the index of the current item within the underlying list data.

Sort

Gets or sets the column names used for sorting and the sort order.


Table 37.3. Commonly Used BindingSource Events

Event

Description

AddingNew

This event is fired before an item is added to the underlying list.

BindingComplete

This occurs when all bound controls have finished binding to this source. This is extremely useful for performing actions that can't be done while binding is still taking place.

CurrentChanged

This event is fired when the Current property is changed. Not to be confused with CurrentItemChanged.

CurrentItemChanged

This event is fired when the Current property changes, or any property on the current item changes.

DataError

This event is fired when a data exception is silently trapped. You can use this to inform your code of the error.

DataMemberChanged

Fired when the DataMember property changes.

DataSourceChanged

Fired when the DataSource property changes.

ListChanged

Fired either when the underlying list changes, or when one of the items in the list changes.

PositionChanged

Fired after the Position property has changed.


Rather than showing you specific examples of how the BindingSource works, you will see the BindingSource in action throughout the rest of the chapter as the different kinds of binding are explained in detail. Because the component acts as an intermediary between bound controls and the underlying data, you can't really see how it works unless you are trying to bind data, and examples of data binding are shown throughout the rest of the chapter.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net