Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoids a System.InvalidCastException error to be thrown when passing ObjectCollection to AddRangeInternal of the DataGridViewComboBoxCell.ObjectCollection class #12613

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,30 @@ public void AddRange(params object[] items)
public void AddRange(ObjectCollection value)
{
_owner.CheckNoDataSource();
AddRangeInternal((ICollection<object>)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);
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Loading