private void dataGridView1_CellValidating(
object sender, DataGridViewCellValidatingEventArgs e)

{
//用于对某一列数据进行验证

dataGridView.Rows[e.RowIndex].ErrorText =
string.Empty;

....
//对每一单元格数据进行验证.

}
private void dataGridView1_DataError(
object sender, DataGridViewDataErrorEventArgs e)

{
//验证触发异常时,显示错误提示
if (!e.ThrowException && e.Exception !=
null)

{

MessageBox.Show(e.Exception.Message,
"系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

dataGridView1[e.ColumnIndex, e.RowIndex].Selected =
true;

dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex, e.RowIndex];

e.Cancel =
true;

}

}
private void dataGridView1_CellEndEdit(
object sender, DataGridViewCellEventArgs e)

{
//停止编辑时,清除所有错误
this.dataGridView1.Rows[e.RowIndex].ErrorText =
string.Empty;

}