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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With