private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DateTime dtime;
string s = dataGridView1.CurrentCell.Value.ToString().Trim();
DateTime s2;
if (e.ColumnIndex == 4)
{
if (DateTime.TryParse(dataGridView1.CurrentCell.Value.ToString().Trim(), out dtime))
{
//dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;//如果改正错误,取消行提示
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = string.Empty;//如果改正错误,取消单元格提示
}
else
{
try
{
s2 = DateTime.ParseExact(s, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = string.Empty;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = s2.ToString("yyyy-M-dd"); //将20110812 变成日期格式 2011-8-12(而不是2011-08-12)
}
catch
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "输入内容不符合规范!";
dataGridView1.CancelEdit();
}
}
}
}
{
DateTime dtime;
string s = dataGridView1.CurrentCell.Value.ToString().Trim();
DateTime s2;
if (e.ColumnIndex == 4)
{
if (DateTime.TryParse(dataGridView1.CurrentCell.Value.ToString().Trim(), out dtime))
{
//dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;//如果改正错误,取消行提示
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = string.Empty;//如果改正错误,取消单元格提示
}
else
{
try
{
s2 = DateTime.ParseExact(s, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = string.Empty;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = s2.ToString("yyyy-M-dd"); //将20110812 变成日期格式 2011-8-12(而不是2011-08-12)
}
catch
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "输入内容不符合规范!";
dataGridView1.CancelEdit();
}
}
}
}