Re: FormatException w/DataGridView when binding to a list of custom o
- From: "Bart Mermuys" <bmermuys.nospam@xxxxxxxxxxx>
- Date: Tue, 14 Nov 2006 21:18:20 GMT
Hi,
"Nejat" <Nejat@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:7B979936-FF51-44A1-8EC5-4E2DD8D5770B@xxxxxxxxxxxxxxxx
I am binding a DataGridView with a List of custom objects which itself has
both intrinsic and custom properties. I accomplish the binding through a
BindingSource instantiated with a BindingList instantiated with the list
of
custom objects. The grid correctly sets up all of the columns, but does
not
allow me to enter a new value for columns that represent custom properties
of
the unterlying object. Intrinsic and string values do not have the same
problem.
The problem occurs after after entering a valid value it the problematic
column, attempting to move off the cell throws a FormatException with a
message that starts with "Invalid cast from 'System.String' to ..."
The offending object type is defined as a struct and implements a
IConvertible and several implicit operators including to and from string
casting.
Any help in the matter would be greatly appreciated.
Try implementing a TypeConverter instead or in addition:
[TypeConverter(typeof(SomeStructConverter))]
public struct SomeStruct
{
....
}
public class SomeStructConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return (destinationType == typeof(string));
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
return (sourceType == typeof(string));
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
// convert from struct to string
SomeStruct st = (SomeStruct) value;
...
return ...
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
// convert from string to struct
string s = (string) value;
...
return ...
}
}
Within the TypeConverter you could also use your struct operators or
IConvertible to do the conversion if you still planning on implementing
those too.
HTH,
Greetings
Nejat
.
- Prev by Date: Re: Container for VisualStyles
- Next by Date: custom control on TabControl
- Previous by thread: IO.Compression and Encryption Error
- Next by thread: Re: FormatException w/DataGridView when binding to a list of custo
- Index(es):
Relevant Pages
|
Loading