您的位置:首页 > 其它

How to bind a GridView to a list of multiple types? NHibernate proxy causing problems with databinding [From stack overflow]

2010-10-12 14:37 891 查看

NHibernate proxy causing problems with databinding

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐