您的位置:首页 > 编程语言 > C#

C# Winform Datagridview中单元格验证输入的内容是否为数字

2016-11-23 14:27 525 查看
1.设置Datagridview的CausesValidation的属性为False

2.代码:

private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
DataGridView grid = (DataGridView)sender;
grid.Rows[e.RowIndex].ErrorText = "";

if (grid.Columns[e.ColumnIndex].Name == "数量")
{
Int32 newInteger = 0;
if (!int.TryParse(e.FormattedValue.ToString(), out newInteger))
{
e.Cancel = true;
grid.Rows[e.RowIndex].ErrorText = "请输入整数";
MessageBox.Show("请输入整数!", ES_FrmWelcome.strSoft, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: