c# - 如何处理数据 GridView 中的数据错误

标签 c# .net error-handling

当我使用重载的类对象加载 ComboBox 的列时,我收到来自 dataGridView 的异常 ToString()方法。
我已经尝试了所有我可以在互联网上找到的方法来防止这个错误,我还有另一个关于 SO 的悬而未决的问题,试图解决这个问题,但是我没有成功。
我收到的最直接的答案是处理错误消息,并防止它加载,在这个程度上我已经搜索了,并创建了这个我认为应该解决问题的方法。

private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{
    anError.Cancel = true;
} 
它有点粗糙,但我相信它应该可以工作,但是当我添加断点时,错误仍然存​​在,并且永远不会中断此功能。我以前从未做过任何错误处理,而且很可能我遗漏了一些东西。
想法?

最佳答案

看看这个...

private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{

MessageBox.Show("Error happened " + anError.Context.ToString());

if (anError.Context == DataGridViewDataErrorContexts.Commit)
{
    MessageBox.Show("Commit error");
}
if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
{
    MessageBox.Show("Cell change");
}
if (anError.Context == DataGridViewDataErrorContexts.Parsing)
{
    MessageBox.Show("parsing error");
}
if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
{
    MessageBox.Show("leave control error");
}

if ((anError.Exception) is ConstraintException)
{
    DataGridView view = (DataGridView)sender;
    view.Rows[anError.RowIndex].ErrorText = "an error";
    view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";

    anError.ThrowException = false;
}
}

阅读此链接:DataGridViewDataErrorEventArgs

关于c# - 如何处理数据 GridView 中的数据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16313733/

相关文章:

java - C# IEnumerable 相当于 java Iterator 的 next() 方法?

c# - Task.Factory.StartNew 与异步方法

c# - 如何在 Visual Studio 2012 解决方案中安装 System.Reactive 扩展?

c# - .NET 中定义的 char struct -- 运算符在哪里?

node.js - 如何捕获像 EADDRINUSE 这样的 node.js/express 服务器错误?

python-3.x - Python PIP导致错误

php - 捕获从 Postgresql 到 PHP 的错误

c# - 多播委托(delegate)的返回类型必须为 void。为什么?

c# - .NET Core 库旁边的 Windows 窗体 GUI 可能吗?

.net - 在 VB.NET for WinForms 的禁用文本框中启用滚动条