DataGridView报错:“System.IndexOutOfRangeException: 索引 0 没有值。 可能是非ui线程访问DataGridView控件造成

时间:2024-03-13 18:05:26

报错信息:

---------------------------
“DataGridView 默认错误”对话框
---------------------------
DataGridView 中发生以下异常:

 

System.IndexOutOfRangeException: 索引 0 没有值。

   在 System.Windows.Forms.CurrencyManager.get_Item(Int32 index)

   在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)

 

要替换此默认对话框,请处理 DataError 事件。
---------------------------
确定   
---------------------------

DataGridView报错:“System.IndexOutOfRangeException: 索引 0 没有值。 可能是非ui线程访问DataGridView控件造成

可能原因:

虽然在winform程序中允许非UI线程访问控件,如下代码:

Control.CheckForIllegalCrossThreadCalls = false;//允许非UI线程访问控件

但是仍然在非ui线程*问DataGridView,并且逻辑类似于

https://www.it610.com/article/1454101.htm

http://www.it1352.com/606779.html

所提到的

这种情况下,就会出现上述报错。

解决方法:使用Control.Invoke,用ui线程去访问DataGridView。