Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
50
Index out of range error when exporting an UltraGrid to Excel
posted

We are using NetAdvantage 10.2  CLR2x (build 10.2.10201.1004) and have an UltraGrid displaying data in a usercontrol that is in a dock pane using an UltraWinDock.UltraDockManager

We added a right-click content menu to export the grid to Excel and PDF.

The export is occurring in a separate thread.

Sometimes as the export is occurring, we are getting an "Index was out of range" error.  We have seen two different stack traces.

It doesn't seem to happen on the first time.  If you export twice (say the user changes data...), it can happen.  But it's not consistent.

First one.  This causes the entire UltraGrid to no longer display (replaced with a red X):

 

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

   at System.Collections.ArrayList.get_Item(Int32 index)

   at Infragistics.Shared.DisposableObjectCollectionBase.GetItem(Int32 index)

   at Infragistics.Win.UltraWinGrid.VisibleRowsCollection.get_Item(Int32 index)

   at Infragistics.Win.UltraWinGrid.RowColRegionIntersectionUIElement.PositionChildElements()

   at Infragistics.Win.UIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)

   at Infragistics.Win.UltraWinGrid.RowColRegionIntersectionUIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)

   at Infragistics.Win.UIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)

   at Infragistics.Win.UltraWinGrid.DataAreaUIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)

   at Infragistics.Win.UIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)

   at Infragistics.Win.UltraWinGrid.UltraGridUIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)

   at Infragistics.Win.UIElement.DrawHelper(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Boolean clipText, Boolean forceDrawAsFocused, Boolean preventAlphaBlendGraphics)

   at Infragistics.Win.UIElement.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Boolean forceDrawAsFocused, Boolean preventAlphaBlendGraphics)

   at Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Size elementSize, Boolean preventAlphaBlendGraphics)

   at Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Size elementSize)

   at Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode)

   at Infragistics.Win.UltraWinGrid.UltraGridUIElement.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode)

   at Infragistics.Win.UltraControlBase.OnPaint(PaintEventArgs pe)

   at Infragistics.Win.UltraWinGrid.UltraGrid.OnPaint(PaintEventArgs pe)

   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.Control.ControlNativeWindow.WndProc(Message& m)

   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Second one (this one does NOT cause a problem with the grid display):
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.Collections.ArrayList.get_Item(Int32 index)
   at Infragistics.Shared.DisposableObjectCollectionBase.GetItem(Int32 index)
   at Infragistics.Win.UltraWinGrid.VisibleRowsCollection.get_Item(Int32 index)
   at Infragistics.Win.UltraWinGrid.RowColRegionIntersectionUIElement.PositionChildElements()
   at Infragistics.Win.UIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)
   at Infragistics.Win.UltraWinGrid.RowColRegionIntersectionUIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)
   at Infragistics.Win.UIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)
   at Infragistics.Win.UltraWinGrid.DataAreaUIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)
   at Infragistics.Win.UIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)
   at Infragistics.Win.UltraWinGrid.UltraGridUIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)
   at Infragistics.Win.UIElement.VerifyChildElements(Boolean recursive)
   at Infragistics.Win.ControlUIElementBase.VerifyIfElementsChanged(Boolean verify, Boolean syncMouseEntered)
   at Infragistics.Win.ControlUIElementBase.get_CurrentCursor()
   at Infragistics.Win.UltraControlBase.get_Cursor()
   at System.Windows.Forms.Control.WmSetCursor(Message& m)
   at System.Windows.Forms.Control.WndProc(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)
Sample code (exporting to Excel):
The grid is called "myGrid", and is in a usercontrol.
       ' handler for the content menu
        Private Sub ExcelDataInLayoutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExcelDataInLayoutToolStripMenuItem.Click
            ' if the temp folder can be found, start a new thread to export the grid data to Excel
            If GetTempFolder() = True Then
                Dim thread As New Thread(AddressOf ThreadProcGridXLS)
                thread.Start()
            End If
        End Sub
        Private Sub ThreadProcGridXLS()
            Try
                ' create a filename with the timestamp
                Dim strFilename As String = (set file name here...)
                ' show message
                StatusDialog.ShowStatus()
                Thread.Sleep(100)
                StatusDialog.SetStatus("Exporting to Excel. Please Wait . . . ")
                Try
                    ' export to Excel
                    UltraGridExcelExporter1.Export(Me.myGrid, strFilename)
                Catch ex As Exception
                    AddLog(ex.ToString, TraceEventType.Error)
                    MessageBox.Show("The Application has failed to export to Excel.")
                End Try
                Try
                    ' try to open the file
                    StatusDialog.CloseForm()
                    Process.Start(strFilename)
                Catch ex As Exception
                    AddLog(ex.ToString, TraceEventType.Error)
                    MessageBox.Show("The Application has failed to load in Microsoft Excel.")
                End Try
            Catch ex As Exception
                AddLog(ex.ToString, TraceEventType.Error)
            End Try
        End Sub
Parents
No Data
Reply
  • 469350
    Offline posted

    skorin said:
    The export is occurring in a separate thread.

    This is almost certainly the problem. You cannot export the grid on a separate thread because the grid and the exporter will be accessing the data source during the export process and this will cross the threads without the proper marshaling.

    Using threading with bound controls is dangerous in general, because you are not in control of the communication between the bound control, the BindingManager, and the data source.

Children
No Data