all groups > dotnet windows forms databinding > january 2005 >
You're in the

dotnet windows forms databinding

group:

refresh of dataGrid bound to a collection when some item changes



refresh of dataGrid bound to a collection when some item changes verane.chanson NO[at]SPAM varianinc.com
1/6/2005 5:19:19 AM
dotnet windows forms databinding: Hi group,

I have a class which inherits from CollectionBase and implements
IBindingList.
I have bound this class to a data grid using the 'datasource'
property.

My class is named 'PersonneCollection' and is a list of 'Personne'.
My 'Personne' class has several fields like name, age...

What I would like is when I change for example the age of one Person,
the corresponding cell is automatically refreshed in the grid.
(Personnne P1 = MyPersonneCollection[1];
P1.Age = 50;)

For the moment the only thing I managed to do is when I change an
existing Personne, the grid is refreshed.
(Personne modified_personne = new Personne("SIMITH", "John", 32);
MyPersonneCollection[1] = modified_personne ;)
To be able to do that, I have overrided the 'OnSetComplete' event in
the 'PersonCollection' class like this :

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

But it is not natural to write something like that when you just want
to change the age of a 'Personne' for example...
What could I do to have my first case works ?? Has the solution
something to deal with 'ICustomDescriptor' ? How can I implement this
interface ?


Thanks by advance,
Re: refresh of dataGrid bound to a collection when some item changes Grant Frisken
1/6/2005 4:35:39 PM
The best way to achieve what you want would be add a Changed event to
your 'Personne' class which you fire from inside the property set.

Your Personne collection can then attach to this event for each of the
Personne objects added to it (by overriding the OnInsertComplete).
Don't forget to detach in the OnClearComplete and OnRemoveComplete.
The event handler then calls OnListChanged.

Typically you would also want a means of suppressing the Changed event
from firing to allow you to make multiple changes without lots of list
updates. You do this by adding SuppressChanges/ResumeChanges methods
which increment/decrement a flag in your Personne class. When the flag
returns to zero you should call the Changed event.
AddThis Social Bookmark Button