views:

251

answers:

1

we have a custom datatype object "Money" which is used to represent money values in our application. at the moment we are trying to implement some custom formatting within a grid component however the exception "InvalidCastException" is raised from System.Convert.

the text from the exception is;

System.InvalidCastException occurred
  Message="Invalid cast from 'System.Int32' to 'System.Money'."
  Source="mscorlib"
  StackTrace: at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
  InnerException:

please note that our "Money" datatype does implement IConvertible as well as having its own TypeConverter.

How can we overcome this exception?

EDIT: This issue is related to DevExpress XtraGrid "Format Conditions" being evaluated on a column bound to our custom datatype "Money". The Money type can be bound to a column without problem, it only raises an InvalidCastException when a format condition is being evaluated on the column, i.e. GridColumnValue > 0

+1  A: 

It sounds like the problem is that the value here is int, and int isn't convertible to Money, since it doesn't know anything about it (but Money might be convertible to int).

Glancing through the code (reflector), there is no obvious way that Convert.ChangeType is ever going to like this usage.

I'd be interested in knowing what grid component you are using, and where the int is coming from, because normally you would expect it to be dealing mainly to/from string, via the TypeConverter associated with the type or property (via PropertyDescriptor.Converter). Since it sounds like you have written your own TypeConverter, it would also be worth showing the ConvertFrom implementation.

Outside the grid usage, in regular C# you should be able to do this simply by define a static conversion operator (implicit / explicit).

Marc Gravell
thanks marc. to get specific, the grid component is DevExpress XtraGrid. We are attempting to implement a format condition based upon a column bound to our Money datatype. For example an expression such as "GridColumnPrice > 0" is raising the InvalidCastException in System.Convert. No matter what code we are implementing, i.e. TypeConverter or IConvertible none of these methods are being called to convert from Int32 to Money. We have static conversion operators in place, Int32 can be converted to Money using our conversion but the grid component is not calling them.
Roboudy
our TypeConverter implementation is used for string to Money and Money to string conversions. When we change/add code to detect an Int32 type, the code is still not being called by the grid.
Roboudy
So it sounds like the problem is the "`GridColumnPrice > 0`"; does it work OK if you remove this? You would have to check XtraGrid documentation to see how this behaves, as it is not a core component, but you could *try* implementing `IComparable<int>`, but I expect that is a bit overly hopeful.
Marc Gravell
everything works fine without the "GridColumnPrice > 0" expression. its only when the grid is attempting to evaluate the expression that it generates the InvalidCastException.
Roboudy
our Money type also implements IComparable<Money> and IComparable.
Roboudy