gpt4 book ai didi

c# - 无法在无效的数据输入上自定义DataGridView中的DataError报表

转载 作者:行者123 更新时间:2023-12-03 08:54:09 25 4
gpt4 key购买 nike

我正在使用通过BindingSource控件连接到数据库表的DataGridView控件。该表包括日期/时间字段。当在与日期/时间字段和提交的行相对应的单元格中输入格式错误的日期时,我看到带屏幕字幕的DataGridView默认错误处理程序,带有异常报告和调用堆栈。底部的消息是“要替换此默认对话框,请处理DataError事件”。如果我向此事件添加代码以执行此操作,则在尝试输入无效数据并提交时,程序将挂起,并且无法将焦点移到包含无效数据的日期/时间单元格中。在这种情况下,我希望能够自定义错误消息。

最佳答案

查找事件中要显示的代码的提示显示了解决方案。我需要将e.Cancel设置为false。没有此代码的代码不会进入事件,这就是为什么我没有列出它的原因

// Don't throw an exception when we're done.
e.ThrowException = false;

// Display an error message.
string txt = "Error with " +
dgvPeople.Columns[e.ColumnIndex].HeaderText +
"\n\n" + e.Exception.Message;
MessageBox.Show(txt, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

// If this is true, then the user is trapped in this cell.
e.Cancel = false;

关于c# - 无法在无效的数据输入上自定义DataGridView中的DataError报表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31735613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com