The procedure is very simple, when the user selects a row of data in the DataGridView control and presses the DELETE key, the Userdeletingrow event is raised. So you can write program code in the Userdeletingrow event handler to display a confirmation dialog box. If the user says not to delete, simply set the Cancel property of the parameter of the Datagridviewrowcanceleventargs type to true.
Figure 12-23
Figure 13-31 shows the execution screen of the program example Ch13_demoform008.cs, which demonstrates how to complete the delete confirmation operation. The program code is listed here as follows:
private void DataGridView1_UserDeletingRow(
object sender, DataGridViewRowCancelEventArgs e)
{
if (!(e.Row.IsNewRow))
{
DialogResult response =
MessageBox.Show("您确定要删除此笔数据行吗?", "请确认",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (response == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true;
}
}
}