Skip to content

DataGridView loses the current cell after a row is removed (regression in .NET 10) #15317

Closed

Description

@krambrock

.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

  1. added
    untriaged
    on Oct 27, 2026
  2. self-assigned this
    on Oct 28, 2026
  3. added
    💥 regression-release
    and removed
    untriaged
    on Oct 28, 2026
  4. added a commit that references this issue on Oct 30, 2026
    6cda066
  5. Olina-Zhang commented on Oct 30, 2026

    @Olina-Zhang
    Member

    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:

    Image
  6. added this to the 12.0-preview1 milestone on Oct 30, 2026
  7. Zheng-Li01 commented on Nov 20, 2026

    @Zheng-Li01
    Member

    Verified the issue with.NET Dec 2026 update test pass build 10.0.16, the issue has been fixed as below as screenshot.

    image
  8. Zheng-Li01 commented on Nov 20, 2026

    @Zheng-Li01
    Member

    Verified the issue with.NET Dec 2026 update test pass build 11.0.1, the issue has been fixed same as above results.

    image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

💥 regression-release

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions