Skip to main content

It is very useful to have a logic for asking user for the confirmation if he/she wants to delete a row in the DataGridView control by hitting the Delete key.

- Advertisement -
- Advertisement -

The event that is rised during a row deleting is the UserDeletingRow so you can use it to change/prevent/confirm default action. This is the appropriate place to implement logic for confirmation, e.g. displaying the MessageBox with info and question.

However, it is recommended that you do this only if the row being deleted is not the new row.

Use the code below and paste it into the UserDeletingRow event handler to show a confirmation dialog on a row delete:

if (!e.Row.IsNewRow)
    DialogResult response = MessageBox.Show("Are you sure?", "Delete row?",
    if (response == DialogResult.No)
      e.Cancel = true;

- Advertisement -