Bonaca
Bonaca

Reputation: 313

How to delete DataGridView Row by pressing DeleteKey?

I have a DataGridView on a form.

I want to delete rows from the grid by pressing the delete key, but delete key seems to be dead, until I enter into CellEditMode.

If I enter to CellEditMode then the delete key works, but obviously only for deleting cell contents, not for deleting rows.

The DataGridView has AllowUserToDeleteRow = true and the grid is unbound.

I have other grids where this function works correctly, but can't see the difference.

Upvotes: 7

Views: 16928

Answers (3)

Julio
Julio

Reputation: 41

If you are using DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter you can't delete any row

Upvotes: 2

blind Skwirl
blind Skwirl

Reputation: 341

The easiest solution to this is making sure "SelectionMode = FullRowSelect" and "EditMode = EditOnKeystrokeOrF2".

This makes it so when they first select the row it doesn't jump into edit mode which would require an "Esc" key press to exit edit mode then delete.

And even though the mode is called "EditOnKeystrokeOrF2", a second click on the cell will also start edit mode.

Upvotes: 1

David Hall
David Hall

Reputation: 33143

One possible cause for the delete functionality not working is that you need to have a row selected.

This means either having the SelectionMode set to FullRowSelect or means that you have previously clicked on the row headers which are to the left of the columns.

Upvotes: 15

Related Questions