Displaying a Collection of Objects in a DataGrid

G

Guest

I am working on an application that is utilizing collections of objects. I
am trying to display the collection of objects in a datagrid. This works
because my collection implements the iList interface. The problem I am
having is that I cannot get the Datagrid to update its contents whenever my
collection object is changed. For example if I add a new object to the
collection or remove an object from the collection, the datagrid contents
remain unchanged.

I set the datagrid.datasource = to my collection, and I have read some
information about having to use databindings to affect any change to the
datagrid, but have been unsuccessful in getting this to work.
 
I

Ilya Tumanov [MS]

That is expected with IList which does not have ListChanged notification
support.

If you want grid and other controls to be updated automatically, you'd need
to implement IBindingList interface on your collection. That would update
grid if items are added or removed.



If you want grid to be updated if item is changed, objects in your
collection would need to implement INotifyPropertyChanged.

Code in your collection should subscribe to PropertyChanged event on each
item in collection and fire ListChanged to notify DataGrid.


--
Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 
G

Guest

Ilya,

Thank you for this information. I have implemented the IBindingList
interface as you suggest, however now, my Collection that implements the
interface is returning a null reference exception in System.dll. Is there
somewhere that demonstrates how to sucessfully implement the IBindingList
Interace in collections?
 
G

Guest

Actually, I have figured out what I was doing wrong, so it is indeed working
now. Thank you for the information.

Guy
 
G

Guest

Ilya,

I have a follow up question about your response. You mentioned to update
the grid when my collection objects change, each object would need to
implement the INotifyPropertyChanged interface. However, I am not finding
any reference to this interface in my environment(VS .Net 2003, c#). Could
that interface be something different?
 
I

Ilya Tumanov [MS]

This interface is new for V2 and is a standard way to receive notifications
of property changes.

If you're using V1, you have to do something so ListChanged event would fire
as you change items in collection, does not have to be this particular
interface.

You could use it as a model anyway:



http://msdn2.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(VS.80).aspx


Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top