DataGridViewRowCancelEventArgs不会触发

时间:2012-05-07 14:51:16

标签: c# sql-server winforms data-binding datagridview

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

我在Windows窗体上有一个绑定到MS SQL表的datgridview。它上面还有一个删除按钮。我想在选择一行并单击按钮时删除一行。做得好。 但是我想在删除操作之前添加一个提醒。但是我发现它不起作用。它永远不会触发事件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;
        }
    }

1 个答案:

答案 0 :(得分:0)

将“使用用户确认”代码移至DeleteRow_Click处理程序。