I have a test app for some grid stuff I'm doing. The app has 4 buttons and allows me to bind different types of data to the grid. I can't actually include all the code involved, but the problem appears to be very isolated.
When I call UltraGridColumn.CalculateAutoResize, from time to time (and there doesn't appear to be a rhyme or reason to it), it throws an IndexOutOfRangeException with this call stack:
at Infragistics.Shared.SparseArray.ValidateIndex(Int32 index) at Infragistics.Shared.SparseArray.GetItem(Int32 index, ICreateItemCallback createItemCallback) at Infragistics.Win.UltraWinGrid.ScrollCountManagerSparseArray.GetItem(Int32 index, Boolean create) at Infragistics.Win.UltraWinGrid.RowsCollection.InternalTraverseRowsHelper(UltraGridBand band, IRowCallback rowCallback, IRowsCollectionCallback rowsCollectionCallback, Boolean recursive, Boolean includeTemplateAddRows) at Infragistics.Win.UltraWinGrid.UltraGridColumn.CalculateMaxCellTextWidth(Int32 maxColWidth, RowsCollection rows, Int32 nRows, Int32 maxRowsCollections) at Infragistics.Win.UltraWinGrid.UltraGridColumn.PerformAutoResizeHelper(RowsCollection rows, Int32 nRows, Boolean applyWidth, Boolean includeHeader, Boolean includeCells, Int32 maxRowsCollections) at Infragistics.Win.UltraWinGrid.UltraGridColumn.PerformAutoResizeHelper(RowsCollection rows, Int32 nRows, Boolean applyWidth, Boolean includeHeader, Boolean includeCells) at Infragistics.Win.UltraWinGrid.UltraGridColumn.CalculateAutoResizeWidth(PerformAutoSizeType autoSizeType, Boolean includeHeader) at DTC.NTFFV.Utilities.UltraGridBindingExtensions.GridExtensions.HandleColumnAttributes(UltraGrid grid, UltraGridColumn col, Type containedType, UltraGridLayout layout) in C:\DTCDev\NTFFV\Utilities\UltraGridBindingExtensions\Domain\GridExtension.cs:line 529
The code in question is:
int calculatedWidth = col.CalculateAutoResizeWidth(PerformAutoSizeType.AllRowsInBand, true) + 8;
Obviously col is a valid UltraGridColumn. What could I possibly be doing that would cause CalculateAutoResize to throw this exception?
Thanks
Pete
As an additional note, the resizing is taking place during the CurrencyManager.ListChanged event. That is, I do something along these lines:
CurrencyManager cm = BindingContext[grid.DataSource, grid.DataMember] as CurrencyManager;cm.ListChanged += new ListChangedEventHander(cm_ListChanged);
then in my cm_ListChanged, I'm looping through all the columns in the grid and performing the autoresize.
I have created a separate app that just does that and I cannot reproduce the error, but it occurs to me that that might be relevant in diagnosing the issue, since I'm sure the grid is also handling the CurrencyManager.ListChanged event.
I appear to have found a solution (leading from my last post). But I'd like some assurnace that this is the correct fix.
Instead of performing the resizing directly in the ListChanged event, I'm doing a SynchronizationContext.Post() call to the method that does the resizing. This seems to avoid the exception. I'm assuming that the grid is performing some work during ListChanged that's not complete until, perhaps, some later event gets called, and doing the auto-resize during this event is catching it off-guard.