经小弟验研总结如下,可以在datagridview CellValidating 事件里写入如下方法:
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int newInteger;
if ((!int.TryParse(e.FormattedValue.ToString(),out newInteger) && !(dataGridView1.CurrentCell.EditedFormattedValue.ToString() == "")) || ((newInteger < 0) && !(dataGridView1.CurrentCell.EditedFormattedValue.ToString() == "")))
{
MessageBox.Show("录入数量:“"+dataGridView1.CurrentCell.EditedFormattedValue.ToString()+"”出错,必须为整数!");
e.Cancel = true;
}
}
//有新的高招请各位过路大侠留下代码,共同学习!