經小弟驗研總結如下,可以在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;
}
}
//有新的高招請各位過路大俠留下代碼,共同學習!