110,536
社区成员
发帖
与我相关
我的任务
分享
前提建数据表绑定DataGridView时,要定义各列的数据类型
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
if (e.Exception != null)
{
DataGridView dgv = (DataGridView)sender;
if (e.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("输入的数量格式不正确。");
}
if (e.Context == DataGridViewDataErrorContexts.CurrentCellChange)
{
MessageBox.Show(e.Exception.Message);
}
if (e.Context == DataGridViewDataErrorContexts.Parsing)
{
MessageBox.Show(e.Exception.Message);
}
if (e.Context == DataGridViewDataErrorContexts.LeaveControl)
{
MessageBox.Show(e.Exception.Message);
}
if ((e.Exception) is ConstraintException)
{
DataGridView view = (DataGridView)sender;
view.Rows[e.RowIndex].ErrorText = e.Exception.Message; ;
view.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = e.Exception.Message;
e.ThrowException = false;
}
dgv.CancelEdit();
}
}