How do I handle editing of custom types in a C# datagridview?

Posted by Ian Hopkinson on Stack Overflow See other posts from Stack Overflow or by Ian Hopkinson
Published on 2010-03-20T11:08:27Z Indexed on 2010/03/20 11:11 UTC
Read the original article Hit count: 228

Filed under:

I have a datagridview in which one column contains a custom class, which I have set using:

dgvPeriods.Columns[1].ValueType = typeof(ExDateTime);

It is rigged up to display correctly by handling the CellFormatting event, but I'm unsure what event to handle for cell editing. In the absence of doing anything I get a FormatException as the datagridview tries to convert String to ExDateTime as I try to move focus out of the edited cell. I tried adding type conversion to my ExDateTime custom class:

public static implicit operator ExDateTime(string b)
    {
        return new ExDateTime(b);
    } 

But this this didn't work. I also tried handling the DataError event, but this seems to fire too late. The datagridview is not databound.

© Stack Overflow or respective owner

Related posts about c#