Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted
Comments
Guirec 12-Feb-13 23:14pm    
Did you try with '==' instead of '=' ?
SuperSh33p 12-Feb-13 23:55pm    
Sorry, it's not clear but I'm currently working in VB.NET, so it's all '='.
Guirec 13-Feb-13 0:07am    
ah ok, sorry.
To solve your issue I suggest you add an additional property to your source object. This new property (let's say you call it MoneycolumConverted) would be the ToString() value (or whatever string representation...) of your Money property then you could write your filter like :

dt.DefaultView.RowFilter = "MoneycolumConverted = 12345.67"

should work...
SuperSh33p 13-Feb-13 0:54am    
Good suggestion.
Unfortunately, I don't think this would work for some of the other filters that I'd ultimately like to add, specifically > (greater than) and < (less than):
"987.65" > "1234567.89" = True

I figured that if I could get equality working, then other operators would be relatively easy.
Guirec 13-Feb-13 0:57am    
ok so don't transform it to a string represnetation but to a Decimal one.

You do it like this:

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

C#
dt.DefaultView.RowFilter = "CONVERT([Money Column], System.String) = '12345.67'"
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900