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
XamDataGrid with intermittent Exception: Index was outside the bounds of the array


I get following exception intermittently and finding it quite difficult to figure out:   

Exception :Index was outside the bounds of the array.
  at Infragistics.Windows.DataPresenter.VirtualizingDataRecordCellPanel.GetFieldsInView()
  at Infragistics.Windows.DataPresenter.VisibleDataBlock.Create(List`1 recordPresenters)
   at Infragistics.Windows.DataPresenter.RecordsInViewHelper.ShouldProcessRecordManager(RecordManager rm, IList`1 fieldInfos)
   at Infragistics.Windows.DataPresenter.RecordsInViewHelper.ShouldProcessInViewRecord(Record record, IList`1 fieldInfos)
   at Infragistics.Windows.DataPresenter.AutoSizeFieldHelper.Calculate(RecordEnumeratorBase outOfViewRecords, AutoSizeCalculationFlags flags, RecordFieldList& recordFieldList, Boolean trackRecords, RecordsInViewHelper recordsInViewHelper)
   at Infragistics.Windows.DataPresenter.AutoSizeFieldHelper.Calculate(RecordCollectionBase records, Int32 recordManagerDepth, AutoSizeCalculationFlags flags, RecordsInViewHelper recordsInViewHelper)
   at Infragistics.Windows.DataPresenter.AutoSizeFieldLayoutInfo.PerformAutoSize(Boolean isHorz, IEnumerable`1 autoFields, RecordCollectionBase records, Int32 recordManagerDepth, HashSet recordsSubset, Boolean increaseOnly, OperationType operationType, FieldAutoSizeOptions allowedOptions, RecordsInViewHelper recordsInViewHelper)
   at Infragistics.Windows.DataPresenter.AutoSizeFieldLayoutInfo.InitializeAutoSizedFieldsImpl(RecordsInViewHelper recordsInViewHelper)
   at Infragistics.Windows.DataPresenter.AutoSizeFieldLayoutInfo.ProcessPendingOperationsImpl(RecordsInViewHelper recordsInViewHelper)
   at Infragistics.Windows.DataPresenter.AutoSizeFieldLayoutInfo.ProcessPendingOperations(List`1 pendingAutoSize, RecordsInViewHelper recordsInViewHelper)
   at Infragistics.Windows.DataPresenter.DataPresenterBase.ProcessPendingAutoSizeOperations()
   at Infragistics.Windows.DataPresenter.DataPresenterBase.OnPerformFieldAutoSize(Object parameter)

The stacktrace starts within the Infragistics component and inspecting the the dll I find that the exception originates from:

internal List<Field> GetFieldsInView()
    List<Field> list = new List<Field>();
    Rect clipRect = this.GetClipRect(base.RenderSize);
    this._lastFieldsInViewClipRect = clipRect;
    if ((this._fieldLayout != null) && (this._cellElementRects != null))
        foreach (Field field in this._fieldLayout.Fields)
            int templateCellIndex = field.TemplateCellIndex;
            if (templateCellIndex >= 0)
                Rect rect2 = this._cellElementRects[templateCellIndex];
                if (!Rect.Intersect(clipRect, rect2).IsEmpty)
    return list;

It looks it is probably from: this._cellElementRects[templateCellIndex]; since this seems to be a place where that type of exception might be thrown.

Since this doesn't happen all the time I am guessing this could be a race condition where the field.TemplateCellIndex is updated to be >=0 but the this._cellElementRects does not have this update yet.  I do have some Visibility bindings to UnboundFields and ticking data so a wild guess would be that the Visibility binding is changing the TemplateCellIndex during the foreach loop. 

I can't quite pin point how this is triggered and how to prevent it.  Any help or ideas on where to start would be much appreciated.