{"record":{"id":"823ffbbae4aefb65","repo":"dotnet/wpf","slug":"sr-gridcollection-mustbecertaintype","errorCode":null,"errorMessage":"SR.GridCollection_MustBeCertainType (ColumnDefinitionCollection, ColumnDefinition)","messagePattern":"SR\\.GridCollection_MustBeCertainType \\(ColumnDefinitionCollection, ColumnDefinition\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":614,"sourceCode":"            if (item.Parent != null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, \"value\", \"ColumnDefinitionCollection\"));\n            }\n        }\n\n        /// <summary>\n        ///     Throws if value is not something the collection expects.\n        ///     Returns true if value belongs to the collection.\n        /// </summary>\n        private bool PrivateValidateValueForRemoval(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            ColumnDefinition item = value as ColumnDefinition;\n\n            if (item == null)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, \"ColumnDefinitionCollection\", \"ColumnDefinition\"));\n            }\n\n            return (item.Parent == _owner);\n        }\n\n        /// <summary>\n        ///     Sets the specified DefinitionBase at the specified index;\n        ///     Connects the value to the model tree;\n        ///     Notifies the DefinitionBase about the event.\n        /// </summary>\n        /// <remarks>\n        ///     Note that the function requires that _item[index] == null and\n        ///     it also requires that the passed in value is not included into another ColumnDefinitionCollection.\n        /// </remarks>\n        private void PrivateConnectChild(int index, DefinitionBase value)\n        {\n            Debug.Assert(value != null && value.Index == -1);\n            Debug.Assert(_items[index] == null);","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L596-L632","documentation":"ArgumentException from ColumnDefinitionCollection.PrivateValidateValueForRemoval: the value being removed is not a ColumnDefinition, so the removal search guard rejects it before scanning the collection.","triggerScenarios":"Calling grid.ColumnDefinitions.Remove(obj) where obj is not a ColumnDefinition (e.g. a RowDefinition or unrelated object). Note: passing a valid ColumnDefinition that is not in the collection does NOT throw — Remove just returns false.","commonSituations":"Mixing up RowDefinitions and ColumnDefinitions in removal code; removing items from an untyped data-bound list.","solutions":["Remove only ColumnDefinition instances","Cast or resolve the item to a ColumnDefinition before calling Remove"],"exampleFix":"// before\ncolumns.Remove(new RowDefinition());\n// after\ncolumns.Remove(columns[0]);","handlingStrategy":"type-guard","validationCode":"if (item is ColumnDefinition cd && cols.Contains(cd)) cols.Remove(cd);","typeGuard":"bool IsRemovable(object o) => o is ColumnDefinition cd && cd.Parent == grid;","tryCatchPattern":"try { cols.Remove(obj); } catch (ArgumentException) { /* wrong type supplied */ }","preventionTips":["Pass typed ColumnDefinition references, not objects","Don't rely on Remove for type filtering — filter with OfType<ColumnDefinition>() first","Distinguish 'wrong type' (throws) from 'not present' (returns false)"],"tags":["wpf","grid","type-mismatch","argument-exception"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}