Show collection of objects in grid

M

Michael C

I wish to show a collection of objects in the DataGridView control. This is
working fairly well except in one particular case. If my collection starts
empty when I assign it to the grid then when an item is added, the item
appears but every cell in the grid is blank. I am assigning the collection
to the grid like this:

MyGrid.DataSource = MyCollection

This works great if the collection contains 1 or more items but in the case
that the user is creating a new collection from scratch then it does not
work. I can see what is happening, the grid doesn't know which columns to
bind to which properties of my object if the collection has not objects. So
far I've managed to get around this by rebinding my collection to the grid
when the first item is added but this seems like a hack. Anyone have a
better solution?

BTW, I create the columns in the grid at design time and have set
AutoGenerateColumns to false.

Thanks,
Michael
 
M

Marc Gravell

What is MyCollection?

WinForms uses various approaches for obtaining metadata (i.e. columns):

[don't worry about these first two; you don't need to know them...]
* if the source implements IListSource, the IList is obtained
* if the source implements ITypedList, this is queried for metadata

then:
* if the source implements IList and has a typed (non-object) indexer
"SomeType this[int]", then the "SomeType" is inspected
* if the source implements IList and is non-empty, the first item is
obtained and inspected
* if the source doesn't implement IList, it might (depending on the
control) get used as a single row
* otherwise you get nada

If you use List<T>, Collection<T>, etc you automatically get the typed
indexer, so this is the simplest fix. Don't use ArrayList.

Marc
 
M

Michael C

Marc Gravell said:
What is MyCollection?

It implements IBindingList but it is my collection so I can do as I need.
WinForms uses various approaches for obtaining metadata (i.e. columns):

[don't worry about these first two; you don't need to know them...]
* if the source implements IListSource, the IList is obtained
* if the source implements ITypedList, this is queried for metadata

then:
* if the source implements IList and has a typed (non-object) indexer
"SomeType this[int]", then the "SomeType" is inspected
* if the source implements IList and is non-empty, the first item is
obtained and inspected
* if the source doesn't implement IList, it might (depending on the
control) get used as a single row
* otherwise you get nada

If you use List<T>, Collection<T>, etc you automatically get the typed
indexer, so this is the simplest fix. Don't use ArrayList.

I'll give it a go, it can't hurt to implement another interface :)

Thanks for your help,
Michael
 
M

Michael C

Marc Gravell said:
Indeed. In that case, it should just need IList plus a typed indexer.

Just IList on its own is no good because you can't raise an event to tell
the grid that items have been added/removed/changed. IBindingList has such
an event (I can't remember what it is called but it's only got 1 event).

Michael
 

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