InvalidCastException in DataGridView

Posted by Max Yaffe on Stack Overflow See other posts from Stack Overflow or by Max Yaffe
Published on 2010-03-24T20:46:32Z Indexed on 2010/03/26 19:53 UTC
Read the original article Hit count: 421

(Using VS 2010 Beta 2 - .Net 4.0 B2 Rel)

I have a class, MyTable, derived from BindingList where S is a struct. S is made up of several other structs, for example:

public class MyTable<S>:BindingList<S> where S: struct
{
    ...
}

public struct MyStruct
{
    public MyReal r1;
    public MyReal r2;

    public MyReal R1 {get{...} set{...}}
    public MyReal R2 {get{...} set{...}}

    ...
}

public struct MyReal
{
    private Double d;

    private void InitFromString(string) {this.d = ...;}

    public MyReal(Double d) { this.d = d;}
    public MyReal(string val) { this.d = default(Double);  InitFromString(val);}

    public override string ToString() { return this.real.ToString();}
    public static explicit operator MyReal(string s) { return new MyReal(s);}
    public static implicit operator String(MyReal r) { return r.ToString();}
    ...
}

OK, I use the MyTable as a binding source for a DataGridView. I can load the data grid easily using InitFromString on individual fields in MyStruct.

The problem comes when I try to edit a value in a cell of the DataGridView. Going to the first row, first column, I change the value of the existing number. It gives an exception blizzard, the first line of which says:

System.FormatException: Invalid cast from 'System.String' to 'MyReal'

I've looked at the casting discussions and reference books but don't see any obvious problems.

Any ideas?

© Stack Overflow or respective owner

Related posts about convert

Related posts about datagridview