I’ve been having a problem lately with DataGridView binding. I have an interface (IAllergen) which is implemented by three different classes (Drug, DrugIngredient, and AllergenGroup). I want to bind a BindingList
My problem with this binding situation is actually two fold. Initially, the implementations of IAllergen were explicit, meaning the interface members only are accessible when explicitly cast as IAllergen. This caused the DataGridView to not think that the object had the specified property, even though the list is typed as List
My next attempt was to make the IAllergen implementations implicit by just providing the appropriate properties on each class. This got past the initial issue, but now during display of a row I get a ‘TargetInvocationException’ with the message “Object does not match target type.” My guess here is that the DataGridView looks at the first object in the bound collection and wants every subsequent item to be of the same type. Is it that difficult for the DataGridView to work for implementations of an interface?
All along I’ve been working with a BindingList
Whew, at least I’ve got a starting place. Now I just need to figure out how to abstract a generic controller class that can use strongly typed collections and return the results back to the UI in an untyped fashion for display. Welcome MVC design practices.