开发者

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

开发者 https://www.devze.com 2022-12-23 07:33 出处:网络
I have a datagridview in which one column contains开发者_如何学Go a custom class, which I have set using:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消