How to display a confirmation dialog box when a user tries to delete a data row in C #

Source: Internet
Author: User
Tags delete key

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;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.