0

Windows 窗体应用程序。C# 4.0。

我有一个 datgridview 绑定到 Windows 窗体上的 MS SQL 表。上面还有一个删除按钮。当我选择一行并单击按钮时,我想删除一行。它做得很好。但我想在删除操作之前添加一个提醒。但是我发现它不起作用。它从不触发事件 dgv_UserDeletingRow。

private void DeleteRow_Click(object sender, EventArgs e)
    {
        try
        {
            DataRowView currentDataRowView = (DataRowView)dgv.CurrentRow.DataBoundItem;
            DataRow row = currentDataRowView.Row;
            // Delete Table here and successfully
        }
        catch (Exception exceptionObj)
        {
            MessageBox.Show(exceptionObj.Message.ToString());
        }

    }

    private void dgv_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
    {
        if (!e.Row.IsNewRow)
        {
            DialogResult res = MessageBox.Show("Are you sure you want to delete this row?", "Delete confirmation",
                     MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == DialogResult.No)
                e.Cancel = true;
        }
    }
4

1 回答 1

0

将“与用户确认”代码移动到DeleteRow_Click处理程序中。

于 2012-05-07T18:42:30.520 回答