我正在创建一个记录员工时间的应用程序。所有用户的所有时间数据都将以UTC格式存储在SQL数据库中。前端将是一个Windows窗体应用程序,它显示时间并接受用户本地时区的输入。 WinForms应用程序有一个DataGridView
控件,用户可以显示和输入时间。
DataGridView
绑定到List
个TimeSegment
个对象。 TimeSegment
是我创建的结构,基本上是这样的:
struct TimeSegment
{
private DateTime start, end;
public TimeSegment(DateTime start, DateTime end){
Start = start;
End = end;
}
public DateTime Start
{
get { return start; }
set {
if (value.Kind != DateTimeKind.Utc)
throw new ArgumentException("Only UTC dates are allowed.");
start = value;
}
}
//There is an End property which is just like the Start
//property, but for the 'end' field.
}
因此在DataGridView
中,会有一列开始时间和一列结束时间,每行代表用户执行的某个事件或任务。
我知道如何使用DataGridView.CellFormatting
事件将内部UTC时间转换为本地时区进行显示。我遇到的问题是在本地时间内接受用户输入并转换为UTC,然后再将更新发送到DataSource
的{{1}}。
我已经看过其他一些帖子,其中显示了如何更改DataGridView
DataGridView
或CellParsing
个事件中单元格中的文字,但我需要更改将文本解析为的实际对象,将其发送到CellValidating
。
有没有办法覆盖单元格的解析行为并指定应该将哪个对象发送到DataSource?
答案 0 :(得分:0)
您可以尝试这样的事情:
private void mainGrid_CellValidated(object sender, DataGridViewCellEventArgs e)
{
//Convert time
(mainGrid.Rows[e.RowIndex].DataBoundItem as TimeSegment).Start = convertedValue;
}