DatagridView Crashes OnPaint. When stepping thru debugger can never be reproduced.-Collection of common programming errors

I’m having a really annoying and serious problem happening with DataGridView…

I have typed dataset in memory wich I fill via a background worker.

Once it finishes sucessfully, I enable some grids to show that data.

On selection changed, I disable all the grids so the user doesen’t click the same grid and try to run the second worker two times at the same time. the second background worker retrieves some data and puts it into the same object on Form1, does some comparisons etc….

The problem is when i click on ANYTHING to change the selection in the first grid… I have no code On_Paint.  I explicitly check for a selected row count to make sure something is there before i read it’s value.

but i get this error:

DataGridView Default Error

IndexOutOfRange Index 0 does not have a value.

Note that I’ve stepped into the debugger and the error DOES NOT occur. Only when I’m not stepping thru it is when it happens, making sure that i stepped into all running threads. I don’t think this is my code’s bug at this point but who knows.

Then i get taken to the dissasembler display with the following error:

“Object reference not set to an instance of an object.” 

  at System.Windows.Forms.DataGridViewRow.GetErrorText(Int32 rowIndex)   at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)   at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)   at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)   at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)   at System.Windows.Forms.Control.WmPaint(Message& m)   at System.Windows.Forms.Control.WndProc(Message& m)   at System.Windows.Forms.DataGridView.WndProc(Message& m)   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)   at System.Windows.Forms.Application.Run(ApplicationContext context)   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

   at Receiving_Specialist.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

this is the only place where it could be but that code needs to be there and it seems safe enough no?

If Me.dg_OrderList.SelectedRows.Count > 0 Then

CurrentOrder =

Me.dg_OrderList.SelectedRows(0).Cells(“OrderNoDataGridViewTextBoxColumn2”).ValueEnd If