C# 如何从数据网格视图处理数据错误

C# 如何从数据网格视图处理数据错误,c#,.net,error-handling,C#,.net,Error Handling,当我使用重载的ToString()方法加载带有类对象的组合框列时,我从dataGridView收到一个异常 为了防止这个错误,我尝试了在互联网上找到的所有方法,我还有一个悬而未决的问题,所以我也试图解决这个问题,但是我没有成功 我得到的最直接的答案是处理错误消息,并阻止加载,在这种程度上,我已经在谷歌搜索过了,并创建了这个方法,我认为应该可以解决这个问题 private void DataGridView1_DataError(object sender, DataGridViewDataErr

当我使用重载的
ToString()
方法加载带有类对象的组合框列时,我从dataGridView收到一个异常

为了防止这个错误,我尝试了在互联网上找到的所有方法,我还有一个悬而未决的问题,所以我也试图解决这个问题,但是我没有成功

我得到的最直接的答案是处理错误消息,并阻止加载,在这种程度上,我已经在谷歌搜索过了,并创建了这个方法,我认为应该可以解决这个问题

private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{
    anError.Cancel = true;
} 
这有点粗糙,但我相信它应该可以工作,但是当我添加断点时,错误仍然存在,并且永远不会进入这个函数。我以前从未做过任何错误处理,很可能我遗漏了什么


想法?

是的,毕竟很简单

private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
需要重新命名

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
各位嘉宾,各位嘉宾:


谢谢你的帮助

是的,毕竟很简单

private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
需要重新命名

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
各位嘉宾,各位嘉宾:

谢谢你的帮助

看看这个

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;
}
}
阅读此链接:

看看这个

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;
}
}

阅读此链接:

您必须在项目中的“YourForm.Designer.cs”文件中插入行代码

在将此方法添加到文件“YourForm.cs”之前


必须在项目中的文件“YourForm.Designer.cs”中插入行代码

在将此方法添加到文件“YourForm.cs”之前


如果你不需要做任何事情,把这个句柄留空,它就会忽略错误。我以前有过这样的经历,没有错误。只是尝试了德里克的建议。它没有起作用。我在Form1类中有一个事件处理函数,它应该在其他地方吗?如果你不需要做任何事情,把这个句柄留空,它就会忽略错误。我以前有过这样的经历,没有错误。只是尝试了德里克的建议。它没有起作用。我在Form1类中有一个事件处理函数,它应该在其他地方吗?