【发布时间】:2018-10-01 19:46:36
【问题描述】:
我正在尝试验证用户在 DataGridViewCell 中输入的 Date 值,如果该值与特定方案不匹配,它应该给用户一个类似
输入的值应匹配 dd/MM/yyyy 格式
我在CellValidating事件上尝试了下面的代码
private void DGV_PatientSessions_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (DGV_PatientSessions.Columns[e.ColumnIndex].Name == "DGV_PatientSessions_Date")
{
string DateValue;
DateTime DateFormated;
DateValue = DGV_PatientSessions.CurrentRow.Cells["DGV_PatientSessions_Date"].Value.ToString();
if (DateTime.TryParseExact(DateValue, "dd/MM/yyyy", new CultureInfo("ar-SY"), DateTimeStyles.None, out DateFormated))
{
MessageBox.Show("done");
}
}
}
但我仍然在下面收到消息错误
我尝试使用我在搜索时发现的不推荐使用的正则表达式,但它不起作用
string DateFormat;
DateFormat = DGV_PatientSessions.CurrentRow.Cells["DGV_PatientSessions_Date"].Value.ToString();
if(Regex.IsMatch(DateFormat, @"(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$"))
{
MessageBox.Show("done");
}
else
{
MessageBox.Show("value should match dd/MM/yyyy format);
}
【问题讨论】:
标签: c# winforms validation datagridview