Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I display a confirmation dialog when the user tries to delete a row?

When the user selects a row in the DataGridView and hits the delete key, the UserDeletingRow event fires. You can prompt the user if they want to continue deleting the row. It is recommended that you only do this if the row being deleted is not the new row. Add the following code to the UserDeletingRow event handler to perform this:

if (!e.Row.IsNewRow)
{
    DialogResult response = MessageBox.Show("Are you sure?", 
          "Delete row?",
          MessageBoxButtons.YesNo, 
          MessageBoxIcon.Question, 
          MessageBoxDefaultButton.Button2);
    if (response == DialogResult.No)
        e.Cancel = true;
}