通过DataTable更新DataGridView时偶尔出现错误 [英] Occasional error when updating DataGridView via DataTable

查看:129
本文介绍了通过DataTable更新DataGridView时偶尔出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C#论坛的质量检查中重新发布此消息,以查看是否可以得到更广泛的答复.

我有一个绑定到数据表的datagridview.我快速且持续地更新数据表(52行,每行每秒更新多次). datatable,datagridview和更新都是在同一线程上完成的,没有更新是并发的,尽管我觉得我的问题是由于GridView将自身从以前的更新更新为Datatable时对DataTable的更新引起的.

通常每隔几分钟,我就会经常出现异常,这是详细信息:
Message =索引超出了数组的范围."
来源="System.Data"
堆栈跟踪:
在System.Data.Common.StringStorage.Get(Int32 recordNo)
在System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetValue(System.Data.DataColumnPropertyDescriptor.GetValue(Object component))(Int32 boundColumnIndex,Int32 columnIndex,Int32 rowIndex)

不管出现什么错误,该程序似乎都可以继续正常运行.这让我发疯,有没有人曾经遇到过这个问题或有任何建议?我唯一的猜测是,我认为我可能会更新数据源的速度如此之快,以至于控件在我再次更新数据源之前还没有完成绘制.如果是这种情况,我有几件事可以尝试:

我真的不想限制更新,因为GUI现在看起来真的很好,但是如果需要的话,我可以.

每当我更新数据表时,我也可以尝试取消绑定并重新绑定数据源.

更新1,我尝试在更新数据表之前先将其锁定在datagridview和datatable上.但是,这没有任何积极作用.

更新2,我可以将自己的处理程序添加到GridView的DataError事件中,但这对调试没有帮助.如果我没有为事件订阅者保留代码,它仍然会获得异常,但是用户永远不会知道(我知道这不是解决方案).

更新3,解决约翰的问题.每当我收到通过套接字连接破解的新价格时,我的数据表都会更新.代码如下所示. Symbol_Feed是字典< string,>

I''m re-posting this in QA from the C# forum to see if I can get a wider response.

I have a datagridview bound to a datatable. I update the datatable quickly and constantly (52 rows, each row updated multiple times per second). The datatable, datagridview, and updates are all done on the same thread and no updates are concurrent, although I feel my issue is caused by updates to the DataTable while the GridView is updating itself from a previous update to the Datatable.

Every so often, usually after a few minutes, I get an exception, here are the details:
Message = "Index was outside the bounds of the array."
Source = "System.Data"
Stack Trace:
at System.Data.Common.StringStorage.Get(Int32 recordNo)
at System.Data.DataColumnPropertyDescriptor.GetValue(Object component) at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)

The program does appear to continue functioning normally regardless of the error. It''s driving me mad, has anyone ever encountered this before or have any advice? My only guess is that I think I may be updating the data source so quickly that the control is not finished drawing before I update the data source again. If this is the case, I have a couple of things to try:

I don''t really want to throttle the updates because the GUI looks really good right now, but I can if needed.

I could try to un-bind and re-bind the datasource every time I update the datatable, too.

Update 1, I have tried putting a lock on the datagridview and the datatable before updating the datatable. However this had no positive effect.

Update 2, I can add my own handler to the DataError event of the GridView, but it doesn''t help with debugging. If I leave out code for the event subscriber it still gets the exception, but the user would never know (I know this is not a solution).

Update 3, Addressing John''s questions. My datatable is updated every time I receive a new price that I crack via a socket connection. Here is what the code looks like. Symbol_Feed is a Dictionary<string,>

this.Symbol_Feed[symbol].SetField("BankVar0", quote.BankVar0);
this.Symbol_Feed[symbol].SetField("BankVar1", quote.BankVar1);
this.Symbol_Feed[symbol].SetField("BankVar2", quote.BankVar2);
this.Symbol_Feed[symbol].SetField("BankVar3", quote.BankVar3);
this.Symbol_Feed[symbol].SetField("BankVar4", quote.BankVar4);
this.Symbol_Feed[symbol].SetField("BankVar5", quote.BankVar5);



我还没有考虑过ObservableCollection对象,但是现在我会考虑.

我不是在添加或删除行,而只是更新现有的.



I had not given the ObservableCollection object any thought yet, but I will now.

I am not adding or deleting rows, only updating existing.

推荐答案

您是通过重新获取数据还是通过在包含的项目中手动设置属性来更新它?

将集合设为ObservableCollection并绑定到该集合是否有帮助?

您要添加/删除整行吗?

Do you update it by re-retrieving the data, or by manually setting properties in the contained items?

Would it help to make the collection an ObservableCollection and binding to that?

Are you adding/deleting entire rows?


尝试在代码周围放置一个try/catch块,然后仅吃掉异常,看看是否至少使代码运行不中断.

如果它是索引超出范围的异常,请找出它在说什么索引并尝试解决它.​​
Try putting a try/catch block around the code, and then just eating the exception, and see if that at least makes the code run without interruption.

If it''s an index out of range exception, find out what index it''s talking about and try to resolve it.


我有一个类似的问题,我只是处理了数据错误事件.
我知道它很粗糙,但似乎可以解决问题.

私有Sub dgv_DataError(ByVal发送者作为对象,ByVal e作为System.Windows.Forms.DataGridViewDataErrorEventArgs)处理dgv.DataError
``这里没有代码
结束Sub
I had a similar problem and I just handled the data error event.
I know it is crude but it seemed to do the trick.

Private Sub dgv_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgv.DataError
''No code here
End Sub


这篇关于通过DataTable更新DataGridView时偶尔出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆