Re: FormatException w/DataGridView when binding to a list of custom o



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


.



Relevant Pages

  • Re: how to store list of varying types
    ... When there's a variable-length string, ... typedef struct { ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ...
    (microsoft.public.vc.mfc)
  • Re: Extending MembershipUser
    ... public override void Initialize(string name, ... public override bool EnablePasswordRetrieval ... public override int MaxInvalidPasswordAttempts ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: MVC in C++
    ... Things are now flowing more along the classic MVC lines. ... > struct Observable ... > string version; ... In that case the Controller would trigger a model update *and it* could ...
    (comp.object)
  • A note on personal corruption as a result of using C
    ... Many people in this ng are personally corrupt and use this ng to take ... "A string cannot contain Nuls" Yes it can. ... "A struct is a class" No, ... It appears that no C maven has read a history of mathematics. ...
    (comp.programming)
  • Re: Redirect msh.exe in/out
    ... private string ExecCmd ... ConsoleColor oldFg = Console.ForegroundColor; ... public override CultureInfo CurrentUICulture ... public override void EnterNestedPrompt() ...
    (microsoft.public.windows.server.scripting)

Loading