How do I handle editing of custom types in a C# datagridview?
I have a datagridview in which one column contains开发者_如何学Go 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.
It turns out I need to handle the CellParsing event:
e.Value = new ExDateTime(e.Value.ToString());
e.ParsingApplied = true;
try handling the grid's CellValidating
event
精彩评论