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

探讨了使用NHibernate查询并绑定到GridView时遇到的问题,特别是当列表中第一个元素为代理对象时出现类型不匹配异常。文章讨论了可能的解决方案,包括关闭NHibernate的代理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值