I'm displaying a list of objects in a DataGridView. Everything was working fine. Columns were automagicaly added to the DataGridView based on the properties of the objects.
Now I changed the class I'm displaying in the grid to implement ICustomTypeDescriptor. But now the grid now no longer shows any columns or rows when I set it's DataSource to a list of my custom object.
I'm guessing this has something to do with the fact that with ICustomTypeDescriptor each instance shown in each row of each grid could be returning a different set of properties.
I'm implementing ICustomTypeDescriptor so that I can allow the users to dynamically add custom properties to objects at run time. These custom properties should be visible and editable through the DataGridView.
Why does DataGridView not see my ICustomTypeDescriptor methods? Is there another way that I can dynamically add properties to an object that will be displayed in a DataGridView?
DataGridView
looks at the list version of metadata; the rules for this are... complex:IListSource
,GetList()
is evaluated and used as the data-source (continue at 2)ITypedList
,GetProperties()
is used to obtain metadata (exit)object
) indexer can be found (i.e.public T this[int index]
), thenT
is used as the source viaTypeDescriptor.GetProperties(type)
:TypeDescriptionProvider
is assigned, the this is used for metadata against the type (exit)TypeDescriptor.GetProperties(list[0])
:ICustomTypeDescriptor
is implemented, then it is used (exit) [*]TypeDescriptionProvider
is assigned, the this is used for metadata against the type (exit) [*]([*]=I can't remember which way around these two go...)
If you are using
List<T>
(or similar), then you hit the "simplest" (IMO) case - #3. If you want to provide custom metadata, then; you best bet is to write aTypeDescriptionProvider
and associate it with the type. I can write an example but it'll take a while (on the train, probably)...Edit: here's an example that uses
ITypedList
; I'll try to tweak it to useTypeDescriptionProvider
instead...Second edit: a full (yet minimal) example using
TypeDescriptionProvider
follows; long code warning...