gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:14:41 26 4
gpt4 key购买 nike

当我使用重载的类对象加载 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/

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