Skip to content

Commit

Permalink
Avoids a System.InvalidCastException error to be thrown when passin…
Browse files Browse the repository at this point in the history
…g `ObjectCollection` to `AddRangeInternal` of the `DataGridViewComboBoxCell.ObjectCollection` class (#12613)

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`

Co-authored-by: Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box) <v-rbossan@microsoft.com>
  • Loading branch information
ricardobossan and Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box) authored Dec 19, 2024
1 parent 58d5c74 commit 8d96e56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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

0 comments on commit 8d96e56

Please sign in to comment.