Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3909
  • Last Modified:

System.ArgumentOutOfRangeException was unhandled

I receive the following error usually when closing the window... it occasionally happens when resizing the grid that sits inside of a tab.

I have several grids on various tabs.  I have made sure every event on each data event checks to make sure there are rows present before continueing.

It is not a trappable event, in debug mode it comes back with the error but not on any line causing the problem, so I don't know what is causing the problem.  I can't put this into a small code snippet, I have no idea where to begin to track this down besides going to everywhere I am looking at datagridview rows.

Here is the error message:

System.ArgumentOutOfRangeException was unhandled
  Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
  Source="mscorlib"
  ParamName="index"
  StackTrace:
       at System.Collections.ArrayList.get_Item(Int32 index)
       at System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)
       at System.Windows.Forms.DataGridView.FlushDisplayedChanged()
       at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
       at System.Windows.Forms.DataGridView.OnColumnCollectionChanged_PreNotification(CollectionChangeEventArgs ccea)
       at System.Windows.Forms.DataGridViewColumnCollection.OnCollectionChanged(CollectionChangeEventArgs ccea, Boolean changeIsInsertion, Point newCurrentCell)
       at System.Windows.Forms.DataGridViewColumnCollection.Clear()
       at System.Windows.Forms.DataGridView.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.TabControl.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
       at libCom.ucProject.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.Form.Dispose(Boolean disposing)
       at ePerformance.frmMain.Dispose(Boolean disposing) in C:\NCS\ePerformance\Source - 2008\ePerformanceMain\frmMain.Designer.vb:line 13
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Form.WmClose(Message& m)
       at System.Windows.Forms.Form.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.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Form.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmSysCommand(Message& m)
       at System.Windows.Forms.Form.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 ePerformance.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
0
jdressing
Asked:
jdressing
  • 4
  • 4
3 Solutions
 
CodeCruiserCommented:
Apparently, the code line is specified in the exception text above
>at ePerformance.frmMain.Dispose(Boolean disposing) in C:\NCS\ePerformance\Source - 2008\ePerformanceMain\frmMain.Designer.vb:line 13

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


These may not be the exact problem spots but may give you some clues.
0
 
jdressingAuthor Commented:
frmMain.Designer.vb: line 13 is just the dispose method:  MyBase.Dispose(disposing)

I am not sure how to find ePerformance.My.MyApplication.Main   ...there is a ePerformance.My.MyApplication, but I do not see a Main function.  This sounds like something the designer created, but there are only 31 lines in the MyApplication class.  ...not sure where line 81 would be.  That was the first thing I looked for, but could not locate.

It is really bazar that I am getting this error on the dispose method.  A little history on where the datagrids are - they all live on tabs inside of a user control, which resides on an MDI form, inside of an MDI container.

When closing the form or the main MDI container form, it gives me this error.  It seems like .Net trap it but not on any line of code for me to see,  You know how an error will usually stop you on a line of code while in Debug mode, in this case it stops me with this error, but not any particular line in the code.  Sometimes when I close, I get this error:  - these happen randomly when I close the form.  Some kind of clean up logic is happening behind the scenes when closing the form that I cannot trap in the code.  In this case it looks like one of the combo box columns in the datagrid is being referenced on shut down and for some reason not going down correctly.  Notice this line: System.Windows.Forms.DataGridView.OnColumnCollectionChanged_PreNotification
Here is the full error:

System.ArgumentOutOfRangeException was unhandled
  Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
  Source="mscorlib"
  ParamName="index"
  StackTrace:
       at System.Collections.ArrayList.get_Item(Int32 index)
       at System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)
       at System.Windows.Forms.DataGridView.FlushDisplayedChanged()
       at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
       at System.Windows.Forms.DataGridView.OnColumnCollectionChanged_PreNotification(CollectionChangeEventArgs ccea)
       at System.Windows.Forms.DataGridViewColumnCollection.OnCollectionChanged(CollectionChangeEventArgs ccea, Boolean changeIsInsertion, Point newCurrentCell)
       at System.Windows.Forms.DataGridViewColumnCollection.Clear()
       at System.Windows.Forms.DataGridView.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.TabControl.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
       at libCom.ucProject.Dispose(Boolean disposing) in C:\NCS\ePerformance\Source - 2008\libCom\ucProject.Designer.vb:line 13
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.Form.Dispose(Boolean disposing)
       at ePerformance.frmMain.Dispose(Boolean disposing) in C:\NCS\ePerformance\Source - 2008\ePerformanceMain\frmMain.Designer.vb:line 13
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Control.Dispose(Boolean disposing)
       at System.Windows.Forms.Form.Dispose(Boolean disposing)
       at ePerformance.mdiMain.Dispose(Boolean disposing) in C:\NCS\ePerformance\Source - 2008\ePerformanceMain\mdiMain.Designer.vb:line 13
       at System.ComponentModel.Component.Dispose()
       at System.Windows.Forms.Form.WmClose(Message& m)
       at System.Windows.Forms.Form.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.DefFrameProc(IntPtr hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Form.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmSysCommand(Message& m)
       at System.Windows.Forms.Form.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.DefFrameProc(IntPtr hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Form.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmNcButtonDown(Message& m)
       at System.Windows.Forms.Form.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 ePerformance.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Thanks,
Jeff
0
 
CodeCruiserCommented:
It has to do something with your datagrid code. Are you disposing the datagrids first before disposing the form? If yes then try by commenting out each datagrid.dispose and trying to run. This may help to isolate the problem. If not disposing the grids then try disposing them before calling the mybase.dispose. Also, is there any particular event or code you have written to execute when columns change or something? It looks to me like some code is trying to execute when the grid columns are modified and that error is generating the error probably. Try all these suggestions please and let me know.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jdressingAuthor Commented:
NOTE: All the bound datagridviews are dynamically built in code and they can have combobox columns that are bound to other tables.  It is possible that a row in the datagridview may get a datavalue not found in the combobox bound table... which could cause this index not found.  Is there a way to handle this error other than the datagridviews  _DataError method ?

I am dynamically adding custom calendar control columns, DataGridViewTextBoxColumn, DataGridViewComboBoxColumn and DataGridViewCheckBoxColumn type columns.  Operationally they all work fine and have no problems, the problem happens when shutting down.  Do I need to add some clean up logic to the disposed method of my user control that holds all the datagrids?   Do I need to go through each datagrid and destroy each column or just columns.clear or just set the datagrid array to nothing  ...the datagridview lives in an array of objects.  I wonder if the garbage collection is not properly handling release of all the objects.  If anyone has any ideas, I will try anything to get this to stabalize.

Thanks,
Jeff
0
 
CodeCruiserCommented:
Many things you can do
1) Enclose all code in try catch blocks
2) Before disposing the form, delete the links between the grids.
3) Dispose the grid which is refering to the table first and then the rest.
0
 
jdressingAuthor Commented:
Everything was already in Try Catch blocks.

I added more destruction logic in the user control to make sure everything got cleaned up.
I also put the MyBase.Dispose inside of a Try Catch block.

The errors stopped happening.
Thanks,
Jeff
0
 
jdressingAuthor Commented:
Before running the Mybase.Dispose, I added more cleanup logic on the user control -> Data grids and that seemed to do the trick.

Thanks,
Jeff
0
 
CodeCruiserCommented:
My Pleasure.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now