Solved

System.ArgumentOutOfRangeException was unhandled

Posted on 2008-10-02
8
3,745 Views
Last Modified: 2012-05-05
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
Comment
Question by:jdressing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 22631922
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
 

Author Comment

by:jdressing
ID: 22632809
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 22632872
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:jdressing
ID: 22632904
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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 22633105
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
 

Author Comment

by:jdressing
ID: 22633342
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
 

Author Closing Comment

by:jdressing
ID: 31502467
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22633428
My Pleasure.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question