Thursday, January 26, 2006

Binding Data to a datagrid.

Binding Data to a datagrid.

1 Declare a datagrid object.
2 Declare a data grid table style.
a. Set the mapping name to the collection name.
b. Add the DataGridTextBoxColumn to the GridColumnStyles of the datagrid.
3 Add the data grid table style to the table styles of the datagrid.
4. Very important, in order to let the datagrid notified the data change and do the proper update of the interface. The collection object which binds to the interface must support the following:

4-a. bool SupportsChangeNotification {get;} must return true.
4-b.event ListChangedEventHandler ListChanged must be implemented.

public event ListChangedEventHandler ListChanged
{
add
{
m_oOnListChanged += value;
}
remove
{
m_oOnListChanged -= value;
}
}
4-c. Expose the OnListChanged method
protected virtual void OnListChanged(ListChangedEventArgs ev)
{
if (m_oOnListChanged != null)
{
m_oOnListChanged(this, ev);
}
}


protected override void OnInsertComplete(int index, object value)
{
//raise the event on ListChanged
//raise event
OnListChanged( new ListChangedEventArgs(ListChangedType.ItemAdded, index ) );
//base.OnInsertComplete (index, value);
}




protected override void OnSetComplete(int index, object oldValue, object newValue)
{
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
}

protected override void OnRemoveComplete(int index, object value)
{

OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
}

The datagrid which binds to the collection class will register the ListChanged event and will be notified if an item is added.

If those properties, methods, events are not properly registered, the datagrid will behave stupid and never responds to any event.

No comments: