I have a helper class that implements ITypedList, to provide objects for databinding against custom collections.
My implementation allows me to easily specify that I want sub-properties of objects to be available for data binding, for instance I can bind to “Id”, “Name”, and also “Children.Count”.
Now, my problem now is that in order to provide helper-objects to data-bind to such columns, I need to pre-populate a collection with those objects, and then later on when a grid asks for such helper objects through the interface, it will call a method on my object to retrieve them.
The method it calls has a parameter that can be used to specify which accessor objects to retrieve, but it’s invariably null for the .NET DataGridView class, which means I just have to return all the objects I got.
So my question is this. Is there an alternative to ITypedList that I can implement where I will be told explicitly which properties the grid is interested in, so that I don’t have to pre-populate the collection of acccessor objects?
Here’s what my code looks like now:
var wrapper = new TypedListWrapper<Person>(someNormalCollectionClass);
wrapper.BindableProperties = "Id;Name;Children.Count";
grid.DataSource = wrapper;
here’s what I want it to look like:
grid.DataSource = new TypedListWrapper<Person>(someNormalCollectionClass);
Any takers?
If you were to create your own grid, then of course you could implement functionality like this, but not with the
DataGridView(or, I would imagine, any current third-party grid control). I would also be wary of the design-time constraints this would place. IfPropertyDescriptorinitialization is what’s causing your concern, then I would suggest loading up the individualPropertyDescriptorobjects with just a name and a type and have actual usage of the object cause it to load any other information that might be required.In general, though, these should be inexpensive operations. If you’re having performance anxiety over your ability to obtain the list of properties in a reasonable amount of time, you may need to examine where the bottleneck is in your architecture and try to find a way around it. Apart from that, though, I don’t see an option for you. The databinding system is, by design, consumer agnostic. There is no means for a binding consumer to indicate whether or not he intends to use a descriptor or not. Having something like that would be a real chicken-or-the-egg headache.