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;
}
}
}
}