{"record":{"id":"6e2697286aabd164","repo":"dotnet/wpf","slug":"sr-tablecollectionoutofrange-columndefinition","errorCode":null,"errorMessage":"SR.TableCollectionOutOfRange","messagePattern":"SR\\.TableCollectionOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":228,"sourceCode":"            }\n            else\n            {\n                return value.Index;\n            }\n        }\n\n        /// <summary>\n        ///     <see cref=\"IList.Insert\"/>\n        /// </summary>\n        /// <remarks>\n        ///     <paramref name=\"value\"/> must be of ColumnDefinition type.\n        /// </remarks>\n        void IList.Insert(int index, object value)\n        {\n            PrivateVerifyWriteAccess();\n            if (index < 0 || index > _size)\n            {\n                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);\n            }\n            PrivateValidateValueForAddition(value);\n            PrivateInsert(index, value as ColumnDefinition);\n        }\n\n        /// <summary>\n        ///     <see cref=\"IList<T>.Insert\"/>\n        /// </summary>\n        public void Insert(int index, ColumnDefinition value)   //  void IList<T>.Insert(int index, T item)\n        {\n            PrivateVerifyWriteAccess();\n            if (index < 0 || index > _size)\n            {\n                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);\n            }\n            PrivateValidateValueForAddition(value);\n            PrivateInsert(index, value);\n        }","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L210-L246","documentation":"The explicit IList.Insert on ColumnDefinitionCollection validates that index is between 0 and _size (inclusive, to allow appending). An index outside that range throws ArgumentOutOfRangeException with SR.TableCollectionOutOfRange.","triggerScenarios":"Calling ((IList)grid.ColumnDefinitions).Insert(i, newColumnDefinition) where i < 0 or i > grid.ColumnDefinitions.Count.","commonSituations":"Computing the insert position from a UI element index without converting to the definitions collection; inserting into an assumed-populated collection (Count 0) at a stored index.","solutions":["Clamp the index: Math.Max(0, Math.Min(index, grid.ColumnDefinitions.Count)).","Use Add() when appending — it avoids index math entirely.","Fix the position computation so it derives from ColumnDefinitions.Count, not a Grid.Children index."],"exampleFix":"// before\n((IList)grid.ColumnDefinitions).Insert(grid.Children.IndexOf(el), colDef);\n// after\nint idx = Math.Max(0, Math.Min(grid.Children.IndexOf(el), grid.ColumnDefinitions.Count));\n((IList)grid.ColumnDefinitions).Insert(idx, colDef);","handlingStrategy":"validation","validationCode":"if (index < 0 || index > grid.ColumnDefinitions.Count)\n    throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":"static bool ValidInsertIndex(int i, int count) => i >= 0 && i <= count;","tryCatchPattern":"try { ((IList)grid.ColumnDefinitions).Insert(index, value); }\ncatch (ArgumentOutOfRangeException) { grid.ColumnDefinitions.Add(def as ColumnDefinition); }","preventionTips":["Clamp insert indices against ColumnDefinitions.Count","Prefer Add() for appends","Don't confuse Grid.Children indices with definition-collection indices"],"tags":["wpf","grid","argument-out-of-range","insert"],"backgroundTag":"index-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}