none
DataGridView gives Index out of range error - but internal to DataGridViewCollumnCollection class RRS feed

  • Question

  • I have a form with a DataGridView on it which works fine most of the time.  But every once in a while one of my users is able to click on it in such a way as to crash the program with an error internal to the DataGridView.  As near as I can tell, none of my code is running at the time.  The stack trace looks like this:

    DefaultSource Critical 3 Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index Unhandled Exception
    DefaultSource Information 0 Stack Trace: 
    DefaultSource Information 0   at System.Collections.ArrayList.get_Item(Int32 index)
       at System.Windows.Forms.DataGridViewColumnCollection.DisplayInOrder(Int32 columnIndex1, Int32 columnIndex2)
       at System.Windows.Forms.DataGridView.UpdateSelectedCellsBlock(Int32 anchorColumnIndex, Int32& oldEdgeColumnIndex, Int32 newEdgeColumnIndex, Int32 anchorRowIndex, Int32& oldEdgeRowIndex, Int32 newEdgeRowIndex)
       at System.Windows.Forms.DataGridView.OnCellSelectMouseMove(HitTestInfo hti)
       at System.Windows.Forms.DataGridView.OnMouseMove(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseMove(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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    The DisplayInOrder code which is in the DataGridViewColumnCollection class  seems to do a test without checking if the column indexes are correct:

    internal bool DisplayInOrder(int columnIndex1, int columnIndex2)
            {
                int displayIndex1 = ((DataGridViewColumn) this.items[columnIndex1]).DisplayIndex;
                int displayIndex2 = ((DataGridViewColumn) this.items[columnIndex2]).DisplayIndex;
                return displayIndex1 < displayIndex2;
            }

    I do have an Error event associated with this grid, but it gets completely ignored - this goes completely up to my last chance handler at the application level.  I am using .Net Framework 4.0 in Visual Basic.  Any suggestions on how to work around this?

    Thanks, Neil


    Neil M. Johnson

    Thursday, May 17, 2018 1:47 AM

All replies

  • Hi Neil M. Johnson,

    Thank you for posting here.

    For your question, it depends on the index which user input. I suggest you to make a judgment. When the index to be non-negative and less than the size of the collection, ask the user to input again.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, May 20, 2018 4:13 PM
    Moderator
  • Hi Wendy, you will note from the call-stack above that nowhere in there is any of my code - it is all Microsoft .net framework.  My user is clicking on the grid or on a cell on the grid which causes the grid control to crash without any way for me to intercept it.

    The code that I show above is from the Microsoft grid code.

    I don't know how but somehow my user is clicking on the grid in such a way as to cause the .net framework code to break simply by moving the mouse.  The user is not inputting an index.  There is no way for me to intercept this.

    Please re-read the call stack above.

    Thanks, Neil




    Neil M. Johnson

    Tuesday, May 22, 2018 12:16 AM
  • Hi, this problem is now appearing even though I have disabled the ability for the user to change column widths, row heights or header row heights.  In addition, a user just experienced this error opening a form, before they had done any work on the form.  The call stack shows the following:

    DefaultSource Critical 3 Value of '-39' is not valid for 'ColumnHeadersHeight'. 'ColumnHeadersHeight' must be greater than or equal to 4.
    Parameter name: ColumnHeadersHeight 
    Exception ArgumentOutOfRangeException, Form MortgagePro, Procedure MyApplication_UnhandledException - 8/7/2018 9:22:55 AM
    Additional Info: Unhandled Exception
    DefaultSource Information 0 Stack Trace: 
    DefaultSource Information 0    at System.Windows.Forms.DataGridView.set_ColumnHeadersHeight(Int32 value)
       at System.Windows.Forms.DataGridView.EndColumnHeadersResize(MouseEventArgs e)
       at System.Windows.Forms.DataGridView.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    The actual height of the grid column headers is set to +56.  The DataGridView is displayed on the second tab of the application, which is not even displayed when the form opens.  How could the headers be resized to a negative number?  Again, as is visible from the call stack above, none of my code is being called.  I also do not set or alter the column heights or widths, they are all fixed widths but can have variable row heights, but not column header heights.  This is also completely random, the application will start fine for weeks and then crash.  Re-opening the program and reloading the same mortgage proceeds to work fine.  This crash used to appear on only one users system in the office but is now crashing on a second one, again intermittently.

    Please pass this on to the developers as it does seem to be happening in the .Net framework code.  We are hoping to release this application as a product, but we cannot do that if it crashes randomly without user input.

    Thanks, Neil


    Neil M. Johnson

    Tuesday, August 7, 2018 9:42 PM