Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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,>

MIDL
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);


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

I am not adding or deleting rows, only updating existing.
Posted
Updated 17-Dec-10 10:05am
v7
Comments
Slacker007 17-Dec-10 14:16pm    
You may get some heat from the community for reposting a question. :)
wizardzz 17-Dec-10 14:18pm    
I plan to delete the forum post, hope that's enough though.
wizardzz 17-Dec-10 14:21pm    
Or does that make it worse? I know everyone loves seeing the "Message Removed" subject.
Pete O'Hanlon 17-Dec-10 15:20pm    
You would have been better updating the forum message to point to this question.
wizardzz 17-Dec-10 15:22pm    
You mean 3 wrongs don't make a right? I keep effing it up.

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?

 
Share this answer
 
Comments
wizardzz 17-Dec-10 14:26pm    
Since posting code in here is not going to format good, do you mind if I respond with an update to the original?
Slacker007 17-Dec-10 14:35pm    
wizardzz: putting your code in the <pre> tags won't work?
wizardzz 17-Dec-10 14:37pm    
yeah, but I hate not being able to see a big code block without scrolling.
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.
 
Share this answer
 
Comments
wizardzz 17-Dec-10 16:02pm    
My update is wrapped in try / catch which never gets triggered. It's the GridView code that triggers the Exception. As mentioned in Update #2, I am able to add a handler to the DataError Event, and subsequently do nothing with it. This lets the program continue to run without interruption.
wizardzz 17-Dec-10 16:05pm    
Oh, also, the index appears to be either the row or column the GridView is attempting to fetch data from, I do not call any of the code that generates this error, it just appears under load the GridView is corrupting itself.
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
 
Share this answer
 
I'm not going to mark it as solved because what I am about to say is not, in my opinion, a great solution. After discussing this with an architect here he told me he had a similar issue when doing something similar. He speculates that GridView just can't keep up, so he uses ListBox or ListView for this situation, especially since users will not be entering or editing any of the data. I guess it can handle the load.

So the question remains, is there any way to use gridview for this much data?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900