http://stackoverflow.com/questions/920227/nhibernate-proxy-causing-problems-with-databinding
I have a gridview that is bound to the result from an nhibernate query. If the first item in the list is edited the following exception is thrown:
System.Reflection.TargetException: Object does not match target type
It appears that the problem is caused by the fact that databinding can't deal with the first item in the list being a subtype of the other items in the list.
What is a nice / correct way to solve this problem? Currently I have had to turn off nhibernates proxying.
Edit: I have another couple of solutions:
- Clone everything in the list (http://steve-fair-dev.blogspot.com/2007/08/databind-object-does-not-match-target.html ) - this doesn't work for me as the object doesn't implement ICloneable
- change the order of items in the list so that the proxy isn't first (http://community.devexpress.com/forums/t/30797.aspx ) - this is so hacky, I don't think I can stoop this low!
But none of these feel right though...
Is the root cause due to a proxy object in the list (from lazy loading) or because the list isn't homogeneous (contains multiple types even if they belong to the same class hierarchy)? The problem with non-homogeneous data sets is a known limitation. See this and this .
I don't think there's a solution other than to not use databinding to populate the grid. That's easy enough if it's read-only.
If the filed is specified limited, you can use linq to select the fileds to generate a new list of obj to bind.