程序先执行CellValidating事件,然后才执行DataError事件 private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Age") { try { int ageInfo = Int32.Parse(e.FormattedValue.ToString()); if (ageInfo < 0 || ageInfo > 100) { dataGridView1.Rows[e.RowIndex].ErrorText = "年龄必须大于0小于100!"; e.Cancel = true; } } catch (Exception ex) { dataGridView1.Rows[e.RowIndex].ErrorText = "年龄必须为数字!"; e.Cancel = true; } } } private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) { if (e.Exception != null && e.Context == DataGridViewDataErrorContexts.Commit) { MessageBox.Show("UserID 不能为空!"); } if (e.Exception != null) { MessageBox.Show(e.Exception.ToString(), "错误提示"); } }