0

I am trying to delete rows from datagridview, when the user selects any row and clicks on delete button , it should ask the user whether he wants to delete the rows?

I am able to delete the rows but I am not sure my control doesnot come to method below:

private void dataGridView1_UserDeletingRow(object sender,DataGridViewRowCancelEventArgs e)
{
    DialogResult usersChoice =
    MessageBox.Show("Are you sure you want to delete the selected signs?\r\n" + dataGridView1.SelectedRows.Count + " signs will be deleted!", "Signs", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

    // cancel the delete event
    if (usersChoice == DialogResult.Cancel)
        e.Cancel = true;
}

I am not sure what show I write in my code so that when user clicks delete button the control comes to the above logic.

Any suggestions?

Thanks.

4

3 回答 3

3

我也遇到了这个问题。事件处理程序已绑定并被调用,但“选定”集合为空。

为了解决这个问题,我不得不将网格的 SelectionMode 更改为“FullRowSelect”。

希望这可以帮助。

于 2013-05-21T18:50:17.353 回答
2

确保事件已注册 在此处输入图像描述

然后实现事件:

private void DataGridView1_UserDeletingRow(object sender, System.Windows.Forms.DataGridViewRowCancelEventArgs e)
{
   DialogResult response = MessageBox.Show("Are you sure you want to delete this row?", "Delete row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
   if ((response == DialogResult.No))
   {
      e.Cancel = true;
   }
}
于 2012-04-06T14:27:20.680 回答
2

您可以在Form.

例如:

public Form1()
{
   InitializeComponents();
   dataGridView1.UserDeletingRow += dataGridView1_UserDeletingRow;
}
于 2012-04-06T14:24:32.940 回答