dotnet/wpf · error · ArgumentException

SR.TableCollectionRangeOutOfRange

Error message

SR.TableCollectionRangeOutOfRange

What it means

Thrown by RowDefinitionCollection.RemoveRange when the requested range extends past the end of the collection: _size - index < count means fewer than 'count' elements exist from 'index' onward. ArgumentException with SR.TableCollectionRangeOutOfRange requires the caller to request an in-bounds range.

Solutions

  1. Clamp count: count = Math.Min(count, rowDefs.Count - index) after validating index
  2. Check index + count <= rowDefs.Count before calling
  3. Re-read Count immediately before computing the range

Example fix

// before
grid.RowDefinitions.RemoveRange(start, removeCount);
// after
removeCount = Math.Min(removeCount, grid.RowDefinitions.Count - start);
if (removeCount > 0) grid.RowDefinitions.RemoveRange(start, removeCount);
Defensive patterns

Strategy: validation

Validate before calling

int safeCount = Math.Min(count, grid.RowDefinitions.Count - index);
if (index >= 0 && index < grid.RowDefinitions.Count && safeCount > 0)
    grid.RowDefinitions.RemoveRange(index, safeCount);

Try / catch

try { grid.RowDefinitions.RemoveRange(index, count); }
catch (ArgumentException) { /* range exceeded Count; clamp and retry */ }

Prevention

When it happens

Trigger: Calling grid.RowDefinitions.RemoveRange(index, count) where index + count > grid.RowDefinitions.Count, e.g. RemoveRange(2, 5) on a 4-row grid.

Common situations: Batch-removing rows sized from a stale Count, arithmetic off-by-one in a resize routine, or removing a computed span that assumed the collection was larger.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5ed9a5d53e991878. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs:308

        ///     The zero-based index of the range of RowDefinitions to remove.
        /// </param>
        /// <param name="count">
        ///     The number of RowDefinitions to remove.
        /// </param>
        public void RemoveRange(int index, int count)
        {
            PrivateVerifyWriteAccess();
            if (index < 0 || index >= _size)
            {
                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(SR.TableCollectionCountNeedNonNegNum);
            }
            if (_size - index < count)
            {
                throw new ArgumentException(SR.TableCollectionRangeOutOfRange);
            }

            PrivateOnModified();

            if (count > 0)
            {
                for (int i = index + count - 1; i >= index; --i)
                {
                    Debug.Assert(   _items[i] != null
                                &&  _items[i].Parent == _owner );

                    PrivateDisconnectChild(_items[i]);
                }

                _size -= count;
                for (int i = index; i < _size; ++i)
                {
                    Debug.Assert(   _items[i + count] != null

View on GitHub (pinned to 81131a70a4)