Skip to content

Commit 8d96e56

Browse files
ricardobossanRicardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)
andauthored
Avoids a System.InvalidCastException error to be thrown when passing ObjectCollection to AddRangeInternal of the DataGridViewComboBoxCell.ObjectCollection class (dotnet#12613)
Avoids a System.InvalidCastException error to be thrown when passing ObjectCollection to AddRangeInternal of the DataGridViewComboBoxCell.ObjectCollection Fixes dotnet#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` Co-authored-by: Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box) <v-rbossan@microsoft.com>
1 parent 58d5c74 commit 8d96e56

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewComboBoxCell.ObjectCollection.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,30 @@ public void AddRange(params object[] items)
9393
public void AddRange(ObjectCollection value)
9494
{
9595
_owner.CheckNoDataSource();
96-
AddRangeInternal((ICollection<object>)value);
96+
97+
InnerAddRange(value);
98+
9799
_owner.OnItemsCollectionChanged();
100+
101+
void InnerAddRange(ObjectCollection items)
102+
{
103+
ArgumentNullException.ThrowIfNull(items);
104+
105+
foreach (object item in items)
106+
{
107+
if (item is null)
108+
{
109+
throw new InvalidOperationException(SR.InvalidNullItemInCollection);
110+
}
111+
112+
InnerArray.Add(item);
113+
}
114+
115+
if (_owner.Sorted)
116+
{
117+
InnerArray.Sort(Comparer);
118+
}
119+
}
98120
}
99121

100122
/// <summary>

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewComboBoxCell.ObjectCollectionTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ public void ObjectCollection_AddRange_AddsItemsInSortedOrder()
9797
_collection[2].Should().Be("C");
9898
}
9999

100+
public void ObjectCollection_AddRange_AddsObjectCollectionCorrectly()
101+
{
102+
DataGridViewComboBoxCell.ObjectCollection items = new(_comboBoxCell) { "Item1", "Item2", "Item3" };
103+
104+
_collection.AddRange(items);
105+
106+
_collection.InnerArray.Count.Should().Be(3);
107+
_collection[0].Should().Be("Item1");
108+
_collection[1].Should().Be("Item2");
109+
_collection[2].Should().Be("Item3");
110+
}
111+
100112
[WinFormsFact]
101113
public void ObjectCollection_AddRange_DoesNotAddItems_WhenExceptionIsThrown()
102114
{

0 commit comments

Comments
 (0)