From 7f53401ced6bb2b0646d464d17facbc84db0d7fc Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Wed, 18 Dec 2024 18:13:10 -0300 Subject: [PATCH] Avoids a System.InvalidCastException error to be thrown when passing ObjectCollection to AddRangeInternal of the DataGridViewComboBoxCell.ObjectCollection Fixes #12612 ## Proposed changes - Avoids a `System.InvalidCastException` error to be thrown when passing `ObjectCollection` to `AddRangeInternal` of the `DataGridViewComboBoxCell.ObjectCollection` class, by: - Modifying the `AddRange(ObjectCollection value)` method - Adding the `AddRangeInternal(ObjectCollection items)` methods - Adds unit test ## Customer Impact - Avoids a possible `System.InvalidCastException` ## Regression? - No ## Risk - Minimal ## Screenshots ### Before ### After ## Test methodology - Unit tests ## Accessibility testing ## Test environment(s) - `10.0.100-alpha.1.24573.1` --- ...taGridViewComboBoxCell.ObjectCollection.cs | 24 ++++++++++++++++++- ...dViewComboBoxCell.ObjectCollectionTests.cs | 12 ++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewComboBoxCell.ObjectCollection.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewComboBoxCell.ObjectCollection.cs index ade7c665765..c284d0449c3 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewComboBoxCell.ObjectCollection.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewComboBoxCell.ObjectCollection.cs @@ -93,8 +93,30 @@ public void AddRange(params object[] items) public void AddRange(ObjectCollection value) { _owner.CheckNoDataSource(); - AddRangeInternal((ICollection)value); + + InnerAddRange(value); + _owner.OnItemsCollectionChanged(); + + void InnerAddRange(ObjectCollection items) + { + ArgumentNullException.ThrowIfNull(items); + + foreach (object item in items) + { + if (item is null) + { + throw new InvalidOperationException(SR.InvalidNullItemInCollection); + } + + InnerArray.Add(item); + } + + if (_owner.Sorted) + { + InnerArray.Sort(Comparer); + } + } } /// diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewComboBoxCell.ObjectCollectionTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewComboBoxCell.ObjectCollectionTests.cs index 311aa17d494..308dc360af9 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewComboBoxCell.ObjectCollectionTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewComboBoxCell.ObjectCollectionTests.cs @@ -97,6 +97,18 @@ public void ObjectCollection_AddRange_AddsItemsInSortedOrder() _collection[2].Should().Be("C"); } + public void ObjectCollection_AddRange_AddsObjectCollectionCorrectly() + { + DataGridViewComboBoxCell.ObjectCollection items = new(_comboBoxCell) { "Item1", "Item2", "Item3" }; + + _collection.AddRange(items); + + _collection.InnerArray.Count.Should().Be(3); + _collection[0].Should().Be("Item1"); + _collection[1].Should().Be("Item2"); + _collection[2].Should().Be("Item3"); + } + [WinFormsFact] public void ObjectCollection_AddRange_DoesNotAddItems_WhenExceptionIsThrown() {