C#WinForm、DataGridViewCell GetFormattedValue的调用过于频繁
我正在学习自定义DataGridViewCell,我希望我的用户只能输入可以解析为DateTime的字符串,否则该值设置为2222-2-22(仅用于测试)。我已经从DataGridViewTextBoxCell子类化,使事情变得简单。我已重写SetValue和GetFormattedValue方法 代码如下。然而,在运行时,GetFormattedValue方法被频繁调用,并且 尝试(转换为ToDateTime)捕获 有一些性能问题。我想我的代码有问题 请帮我检查一下,谢谢C#WinForm、DataGridViewCell GetFormattedValue的调用过于频繁,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在学习自定义DataGridViewCell,我希望我的用户只能输入可以解析为DateTime的字符串,否则该值设置为2222-2-22(仅用于测试)。我已经从DataGridViewTextBoxCell子类化,使事情变得简单。我已重写SetValue和GetFormattedValue方法 代码如下。然而,在运行时,GetFormattedValue方法被频繁调用,并且 尝试(转换为ToDateTime)捕获 有一些性能问题。我想我的代码有问题 请帮我检查一下,谢谢 class WMZD
class WMZDGVDateCell : DataGridViewTextBoxCell
{
protected override bool SetValue(int rowIndex, object value)
{
if (value != null) System.Diagnostics.Debug.WriteLine("------in SetValue-------" + value.ToString());
DateTime valueAsDate;
try
{
valueAsDate = Convert.ToDateTime(value);
}
catch
{
valueAsDate = new DateTime(2222, 2, 22);
//throw;
}
return base.SetValue(rowIndex, valueAsDate.ToShortDateString());
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value != null) System.Diagnostics.Debug.WriteLine("------in GetFormattedValue-------" + value.ToString());
DateTime valueAsFormatted;
try
{
valueAsFormatted = Convert.ToDateTime(value);
}
catch
{
valueAsFormatted = new DateTime(2222, 2, 22);
}
return base.GetFormattedValue(valueAsFormatted.ToShortDateString(), rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
}
我会避免使用
Try
Catch
块,除非它们是必需的,因为它们会影响程序的性能。以下是我想到的:
class WMZDGVDateCell : DataGridViewTextBoxCell
{
protected override bool SetValue(int rowIndex, object value)
{
if (value != null) System.Diagnostics.Debug.WriteLine("------in SetValue-------" + value.ToString());
DateTime valueAsDate;
if (value != null && DateTime.TryParse(value.ToString(), out valueAsDate))
{
//succeeded
}
else
{
valueAsDate = new DateTime(2222, 2, 22);
//failed
}
return base.SetValue(rowIndex, valueAsDate.ToShortDateString());
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value != null) System.Diagnostics.Debug.WriteLine("------in GetFormattedValue-------" + value.ToString());
DateTime valueAsFormatted;
if (value != null && DateTime.TryParse(value.ToString(), out valueAsFormatted))
{
//succeeded
}
else
{
valueAsFormatted = new DateTime(2222, 2, 22);
//failed
}
return base.GetFormattedValue(valueAsFormatted.ToShortDateString(), rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
}
您可以做的另一件事是使用DataGridView
CellEndEdit
事件,检查该值是否有效,如果无效,将其更改为默认值