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

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

![Captura de tela 2024-12-09
200542](https://github.com/user-attachments/assets/c5b9fa72-cb93-4c67-b17e-9ffb88a3fa0d)

### After

![image](https://github.com/user-attachments/assets/bc52a015-79f0-40e3-b4e5-0b2b3d095893)

## Test methodology

- Unit tests

## Accessibility testing

## Test environment(s)

- `10.0.100-alpha.1.24573.1`

 ###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/12613)
  • Loading branch information
Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box) committed Dec 10, 2024
1 parent 41932f7 commit baa8edf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void AddRange(params object[] items)
public void AddRange(ObjectCollection value)
{
_owner.CheckNoDataSource();
AddRangeInternal((ICollection<object>)value);
AddRangeInternal(value);
_owner.OnItemsCollectionChanged();
}

Expand All @@ -120,6 +120,26 @@ internal void AddRangeInternal(ICollection<object> 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);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit baa8edf

Please sign in to comment.