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..3e0a33092fe 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,7 +93,7 @@ public void AddRange(params object[] items) public void AddRange(ObjectCollection value) { _owner.CheckNoDataSource(); - AddRangeInternal((ICollection)value); + AddRangeInternal(value); _owner.OnItemsCollectionChanged(); } @@ -120,6 +120,26 @@ internal void AddRangeInternal(ICollection items) } } + internal void AddRangeInternal(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); + } + } + internal void SortInternal() => 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 new file mode 100644 index 00000000000..75156c233ba --- /dev/null +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewComboBoxCell.ObjectCollectionTests.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +namespace System.Windows.Forms.Tests; + +public class DataGridViewComboBoxCell_ObjectCollectionTests : IDisposable +{ + private readonly DataGridViewComboBoxCell _comboBoxCell; + private readonly DataGridViewComboBoxCell.ObjectCollection _collection; + + public void Dispose() => _comboBoxCell.Dispose(); + + public DataGridViewComboBoxCell_ObjectCollectionTests() + { + _comboBoxCell = new(); + _collection = new(_comboBoxCell); + } + + [WinFormsFact] + 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"); + } +}