Monday, September 19, 2011

Windows Forms Data Binding - Data Binding Interfaces


.NET provides a standard set of interfaces related to data binding. Each of these interfaces is described next:
IList
Any class that implements the IList interface must support a list of homogenous types. That is, all list items must be of the same type. The first item in the list always determines the type. Some of the base classes that implement IList include Array, ArrayList, CollectionBase, DataView, and DataViewManager.
Typed IList
Similar to IList, the list must be of homogenous types. However, the type of the items in the list must be known at compile time.
IList and IComponent
When a class implements both IList and IComponent, the class can be data bound at design time.
IListSource
This interface allows an object to “act” like a list for data binding purposes. The implemented object is not an instance of IList, but it should be able to provide one. The DataSet and DataTable classes both implement this interface. IListSource provides a single property and a single method, which are described next:
ContainsListCollection: Indicates whether the collection is a collection of IList objects. For the DataSet implementation, this property returns True, because the DataSet class contains a collection of collections. For the DataTable implementation, this property returns False, because the DataTable class contains a collection of objects. In simple terms, implement this property to indicate how deep to go for returning a bindable list.

GetList: Returns the IList object that will be data-bound. The DataSet class uses this property to return a DataViewManager object. The DataTable class uses this property to return a DataView object.
ITypedList
This interface allows a collection object to expose its items’ properties. This interface is useful in situations where the public properties of the collection object should be different from the properties available for data binding. This interface is also necessary during complex binding when a list is empty but you still need to know the property names of the list items. (Remember, the IList class alone uses the data type of the first item in the list.) This is useful when columns headers should be created for empty lists.
IBindingList
This interface offers change notification when the list or list items have changed. There is one property, SupportsChangeNotification, which indicates whether this interface’s ListChanged event should be raised. The ListChangedEventArgs class contains a property named ListChangedType that describes the type of change that occurred. The available ListChangedType enumeration members are as follows:
ItemAdded: An item has been added to the list. The index of the new item is the value of the NewIndex property of the ListChangedEventArgs class.
ItemChanged: An item in the list has been changed. The index of the changed item is the value of the NewIndex property of the ListChangedEventArgs class.
ItemDeleted: An item has been removed from the list. The index of the deleted item is the value of the NewIndex property of the ListChangedEventArgs class.

ItemMoved: An item has been moved to another location within the list. The previous index is the value of the OldIndex property of the ListChangedEventArgs class. The new index is the value of the NewIndexproperty of the ListChangedEventArgs class.
PropertyDescriptorAdded: A PropertyDescriptor object has been added.
PropertyDescriptorChanged: A PropertyDescriptor object has been changed.
PropertyDescriptorDeleted: A PropertyDescriptor object has been deleted.
Reset: The list has a lot of changes and controls should refresh themselves.
IEditableObject
This interface supports transaction-like operations. It allows objects to specify when changes should be made permanent. Hence, it allows changes to be rolled back. The DataGrid control is one control that opts to call methods of this interface. The following methods are defined in this interface:
BeginEdit: Signals that an edit operation has started. Any changes to the object should be temporarily stored after this method has been called. When implementing this method, be sure that back-to-back calls are non-destructive. That is, the method itself should not cause any changes to any temporary objects.
CancelEdit: Cancels any changes made after the BeginEdit call. In other words, all temporary objects can be destroyed when this method is called.
EndEdit: Commits any changes made after the BeginEdit call. Once this method is called, changes cannot and should not be rolled back.

The following example illustrates the
IEditableObject class with an implementation of a Customer class.
using System.ComponentModel;
public class Customer : IEditableObject
{
 
    private bool _transactionStarted = false;
 
    private string _originalFirstName;
 
    private string _originalLastName;
 
    private string _originalPhoneNumber;
 
    public string FirstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            _firstName = value;
        }
    }
 
    public string LastName
    {
        get
        {
            return _lastName;
        }
        set
        {
            _lastName = value;
        }
    }
 
    public string PhoneNumber
    {
        get
        {
            return _phoneNumber;
        }
        set
        {
            _phoneNumber = value;
        }
    }
 
    public void BeginEdit()
    {
        if (!_transactionStarted)
        {
            _transactionStarted = true;
            _originalFirstName = _firstName;
            _originalLastName = _lastName;
            _originalPhoneNumber = _phoneNumber;
        }
    }
 
    public void CancelEdit()
    {
        if (_transactionStarted)
        {
            _transactionStarted = false;
            _firstName = _originalFirstName;
            _lastName = _originalLastName;
            _phoneNumber = _originalPhoneNumber;
        }
    }
 
    public void EndEdit()
    {
        if (_transactionStarted)
        {
            _transactionStarted = false;
            _originalFirstName = "";
            _originalLastName = "";
            _originalPhoneNumber = "";
        }
    }
}
IDataErrorInfo
This interface offers custom error information to which controls can bind. During data binding, this allows controls to retrieve specific error information from the data source itself. For example, if a particular column in a DataTable object has an Integer type, setting a field to a string value for this column will cause the data source to return an appropriate error. This interface provides the following two properties:
Error: Returns an error message indicating what is wrong.
Item: An indexer that gets the error message for the specified column name or property name.

1 comment :

  1. On of these issues that is really important to consider is notification enabled collections.
    data binding its a good technique to get overcome from it.

    ReplyDelete