为自定义类型设置DataTable RowFilter [英] Set DataTable RowFilter for a custom type

查看:86
本文介绍了为自定义类型设置DataTable RowFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的'Money'类型,我在DataGrid中显示。因此,我绑定到DataGrid.DataSource的数据表中的列被定义为Money类型。我希望能够使用DataTable.DefaultView.RowFilter使用以下代码过滤DataGrid的内容:



 dt.DefaultView。 RowFilter =[Money Column] = 12345.67





但是,无论何时执行此代码,我都会收到以下错误:



发生System.Data.EvaluateException

消息=无法对NAB.WM.CIMS.BusinessObjects.Money和System执行'='操作。十进制。




我以为我可以通过在我的Money类中添加一个新的=运算符来解决这个问题,它将Money与Decimal进行比较,但这个运算符被完全忽略。



似乎正在执行比较的代码是 System.Data.BinaryNode.EvalBinaryOp

有什么方法可以扩展这个类/方法以允许我使用Money类型,还是有其他解决方案我缺少?



谢谢!

解决方案

你这样做:



http://stackoverflow.com/questions/5125373/how-can-i-cast-dataview-rows-to-another-data-type [ ^ ]



 dt.DefaultView.RowFilter =    CONVERT([Money Column],System.String)='12345.67' 


< blockquote>我猜你现在有一个有效的解决方案,所以我在这里添加一些文字,这样你的问题就不再显示为没有回答了



正如PIEBALDconsult所说的你非常有限,你可以在RowFilter中做什么,但还有一件事你可以尝试:如果你有能力修改NAB.WM.CIMS.BusinessObjects.Money类然后y您可以尝试使用Decimal和String实现自定义转换,并覆盖=运算符。也可以工作。但最简单的解决方案仍然是我给你的那个。


I have a custom 'Money' type that I display in a DataGrid. The column in the datatable that I bind to the DataGrid.DataSource is therefore defined as type Money. I want to be able to filter the contents of the DataGrid using DataTable.DefaultView.RowFilter using code like the following:

dt.DefaultView.RowFilter = "[Money Column] = 12345.67"



However whenever this code executes I'm getting the following error:

System.Data.EvaluateException occurred
Message=Cannot perform '=' operation on NAB.WM.CIMS.BusinessObjects.Money and System.Decimal.


I thought I could fix this simply by adding a new "=" operator to my Money class which compares Money with Decimal, but this operator is being completely ignored.

The code that appears to be executing the compare is System.Data.BinaryNode.EvalBinaryOp.
Is there any way I can extend this class/method to allow me to use the Money type, or is there another solution that I'm missing?

Thanks!

解决方案

You do it like this:

http://stackoverflow.com/questions/5125373/how-can-i-cast-dataview-rows-to-another-data-type[^]

dt.DefaultView.RowFilter = "CONVERT([Money Column], System.String) = '12345.67'"


I guess you have a working solution now so I add some text here so that your question does not appear as "not answered" anymore

As mentioned by PIEBALDconsult you are very limited to what you can do in a RowFilter but there is an additional thing you can try : if you have the ability to modify the NAB.WM.CIMS.BusinessObjects.Money class then you can try to implement custom conversion with Decimal and String and also override the = operator. Might work as well. But the simplest solution remains the one I gave you above.


这篇关于为自定义类型设置DataTable RowFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆