[英]How do I handle editing of custom types in a C# datagridview?
I have a datagridview in which one column contains a custom class, which I have set using: 我有一个datagridview,其中一列包含一个自定义类,该类已使用以下方法设置:
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. 它通过处理CellFormatting事件来正确显示,但是我不确定要为单元格编辑处理哪个事件。 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.
在没有做任何事情的情况下,当我尝试将焦点移出编辑单元格时,datagridview尝试将String转换为ExDateTime时,我得到了FormatException。 I tried adding type conversion to my ExDateTime custom class:
我尝试将类型转换添加到我的ExDateTime自定义类中:
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.
我也尝试处理DataError事件,但这似乎为时已晚。 The datagridview is not databound.
datagridview没有数据绑定。
尝试处理网格的CellValidating
事件
It turns out I need to handle the CellParsing event: 原来我需要处理CellParsing事件:
e.Value = new ExDateTime(e.Value.ToString());
e.ParsingApplied = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.