DataGridView 输入数据验证格式(实例)
2013-06-04 21:25
330 查看
在DataGridView属性里面添加dgvOne_CellValidating事件,然后根据需要以后。
private void dgvOne_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
//可编辑的列2、3、4、5、6(实际显示的列减1)列需要输入0~9的自然数.
if ( e.ColumnIndex == 2 || e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 6)
{
int outDb = 0;
if (int.TryParse(e.FormattedValue.ToString(), out outDb))
{
e.Cancel = false;
}
else
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("请输入0~9的自然数!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvOne.CancelEdit();
}
}
//第二列验证时间格式
if (e.ColumnIndex == 1)
{
DateTime outDb =DateTime.Now;
if (DateTime.TryParse(e.FormattedValue.ToString(), out outDb))
{
e.Cancel = false;
}
else
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("输入日期格式不正确,请重新输入!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvOne.CancelEdit();
}
}
//第一列验证正整数
if (e.ColumnIndex == 0)
{
Regex notWholePattern = new Regex(@"^[0-9]\d*$");
if (notWholePattern.IsMatch(e.FormattedValue.ToString(), 0))
{
e.Cancel = false;
}
else
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("输入序号的格式不正确,请重新输入正整数!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvTwo.CancelEdit();
}
}
//长度验证
if (e.ColumnIndex == 6 || e.ColumnIndex == 7)
{
string str = e.FormattedValue.ToString();
int leng = str.Length;
if (leng > 1)
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("只能输入一位0~9的自然数,请重新输入!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvTwo.CancelEdit();
}
else
{
e.Cancel = false;
}
}
}
private void dgvOne_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
//可编辑的列2、3、4、5、6(实际显示的列减1)列需要输入0~9的自然数.
if ( e.ColumnIndex == 2 || e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 6)
{
int outDb = 0;
if (int.TryParse(e.FormattedValue.ToString(), out outDb))
{
e.Cancel = false;
}
else
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("请输入0~9的自然数!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvOne.CancelEdit();
}
}
//第二列验证时间格式
if (e.ColumnIndex == 1)
{
DateTime outDb =DateTime.Now;
if (DateTime.TryParse(e.FormattedValue.ToString(), out outDb))
{
e.Cancel = false;
}
else
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("输入日期格式不正确,请重新输入!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvOne.CancelEdit();
}
}
//第一列验证正整数
if (e.ColumnIndex == 0)
{
Regex notWholePattern = new Regex(@"^[0-9]\d*$");
if (notWholePattern.IsMatch(e.FormattedValue.ToString(), 0))
{
e.Cancel = false;
}
else
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("输入序号的格式不正确,请重新输入正整数!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvTwo.CancelEdit();
}
}
//长度验证
if (e.ColumnIndex == 6 || e.ColumnIndex == 7)
{
string str = e.FormattedValue.ToString();
int leng = str.Length;
if (leng > 1)
{
e.Cancel = true;//数据格式不正确则还原
MessageBox.Show("只能输入一位0~9的自然数,请重新输入!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvTwo.CancelEdit();
}
else
{
e.Cancel = false;
}
}
}
相关文章推荐
- 简单谈谈如何在DataGridView控件中验证数据输入的正确性,winform
- 验证DataGridView控件的数据输入
- SpringMVC数据验证——注册用户格式的验证实例
- SpringMVC数据验证——注册用户格式的验证实例
- 验证DataGridView控件的数据输入
- C#——小编浅谈如何在DataGridView控件中验证数据输入
- ASP.NET 使用RegularExpressionValidator控件(数据输入格式验证)
- 以申购单为实例,讲解Jquery动态删减行,新增行添加鼠标事件,子窗口与父窗口传值,自动计算金额,及输入值的验证,前台数据批量提交到后台action
- DataGridView中数据输入验证
- Android EditView输入内容格式验证
- Angular2管道Pipe及自定义管道格式数据用法实例分析
- EXCEL规范数据输入教程-验证身份证号
- jQuery Ajax 跨域下调用webservice返回jsonp格式数据实例
- js各种验证文本框输入格式(正则表达式)
- 有五个学生,每个学生有3门课的成绩, 从键盘输入以上数据(包括姓名,三门课成绩), 输入的格式:如:zhagnsan,30,40,60计算出总成绩, 并把学生的信息和计算出的总分数高低顺序存放在磁盘文
- C# 文本输入限制类型,datagridview单元格输入验证
- js各种验证文本框输入格式
- Hadoop自定义数据类型和输入格式
- 实例解析嵌套的JSON格式数据
- 通过scanner实现日期格式的数据输入