Repository navigation
Description
.NET version
.NET 10.0.12 (net10.0-windows), the code is the same in main
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
Yes, .NET 9 (9.0.20) and .NET 8 keep a current cell
Issue description
When a row is removed from a DataGridView, the grid no longer moves the current cell to a neighbouring row. After the user deletes the current row with the Delete key, CurrentCell and CurrentRow are null and no row is selected. Pressing Delete again or typing does nothing until the user clicks a row. Rows.RemoveAt on an unbound grid behaves the same.
The cause is a change in DataGridViewRowCollection.RemoveAtInternal by #12534 (Finish enabling unnecessary assignment analyzer):
- Point newCurrentCell = new(-1, -1);
...
- DataGridView.OnRemovingRow(index, out newCurrentCell, force);
+ DataGridView.OnRemovingRow(index, out _, force);
...
- OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewRow), index, 1, true, false, false, newCurrentCell);
+ OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, dataGridViewRow), index, 1, true, false, false, new Point(-1, -1));OnRemovingRow clears the current cell and returns the new one (the next visible row, or the previous one), and OnRemovedRow_PostNotification used to set it. Since the returned value is discarded, the current cell stays cleared. The analyzer only flagged the unused initializer new(-1, -1); keeping out newCurrentCell and passing it on restores the behaviour.
Removing an item from a bound BindingList outside a user deletion still ends with a current cell, because the grid matches the CurrencyManager position afterwards.
Workaround for user deletions: a derived grid remembers CurrentCellAddress in OnUserDeletingRow and sets CurrentCell again in OnUserDeletedRow.
Steps to reproduce
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net9.0-windows;net10.0-windows</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>using System.ComponentModel;
internal static class Program {
[STAThread]
private static void Main() {
Console.WriteLine($".NET {Environment.Version}");
Check("bound, user presses Delete", grid => grid.PressDelete());
Check("bound, item removed from the list", grid => ((BindingList<Item>)grid.DataSource!).RemoveAt(1));
Check("unbound, Rows.RemoveAt", grid => grid.Rows.RemoveAt(1), bound: false);
}
private static void Check(string what, Action<TestGrid> delete, bool bound = true) {
using Form form = new();
TestGrid grid = new() { Dock = DockStyle.Fill };
form.Controls.Add(grid);
if (bound) {
grid.DataSource = new BindingList<Item> { new("A"), new("B"), new("C") };
} else {
grid.Columns.Add("Name", "Name");
grid.Rows.Add("A");
grid.Rows.Add("B");
grid.Rows.Add("C");
}
form.Show();
grid.CurrentCell = grid.Rows[1].Cells[0];
delete(grid);
string current = grid.CurrentCell is null ? "null" : $"row {grid.CurrentCell.RowIndex}";
Console.WriteLine($"{what,-36} CurrentCell: {current,-6} SelectedRows: {grid.SelectedRows.Count}");
}
}
internal sealed class Item(string name) {
public Item() : this("") { }
public string Name { get; set; } = name;
}
internal sealed class TestGrid : DataGridView {
public TestGrid() {
SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
public void PressDelete() => ProcessDeleteKey(Keys.Delete);
}dotnet run -f net9.0-windows and dotnet run -f net10.0-windows:
.NET 9.0.20
bound, user presses Delete CurrentCell: row 1 SelectedRows: 1
bound, item removed from the list CurrentCell: row 1 SelectedRows: 1
unbound, Rows.RemoveAt CurrentCell: row 1 SelectedRows: 1
.NET 10.0.12
bound, user presses Delete CurrentCell: null SelectedRows: 0
bound, item removed from the list CurrentCell: row 1 SelectedRows: 1
unbound, Rows.RemoveAt CurrentCell: null SelectedRows: 0
Expected: the row after the removed one is current and selected, as in .NET 9.
Activity
Fix DataGridView losing the current cell after row removal (dotnet#15321
Olina-Zhang commentedon Oct 30, 2026
Verified the issue with private dlls built from main branch of dotnet/winforms repo + .NET 11 11.0.0 SDK build, it has been fixed as below:
[release/10.0] Fix DataGridView losing the current cell after row rem…
[release/11.0] Fix DataGridView losing the current cell after row rem…
Zheng-Li01 commentedon Nov 20, 2026
Verified the issue with.NET Dec 2026 update test pass build 10.0.16, the issue has been fixed as below as screenshot.
Zheng-Li01 commentedon Nov 20, 2026
Verified the issue with.NET Dec 2026 update test pass build 11.0.1, the issue has been fixed same as above results.