1.验证中用到的事件
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;
}
2.提交数据时用到的事件
this.Validate();//进行自动验证
this.dataGridView1.EndEdit();//结束编辑
对datagridview1每一行,每一列进行验证
提交数据