簡體   English   中英

datagridview 刪除 C# 中的行?

[英]datagridview delete rows in C#?

我試圖從 datagridview 中刪除行,當用戶選擇任何行並單擊刪除按鈕時,它應該詢問用戶是否要刪除這些行?

我可以刪除這些行,但我不確定我的控制權不會轉到下面的方法中:

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

我不確定我在代碼中寫了什么,以便當用戶單擊刪除按鈕時,控件會出現上述邏輯。

有什么建議么?

謝謝。

我也有這個問題。 事件處理程序已綁定並被調用,但“已選擇”集合為空。

要修復它,我必須將網格的 SelectionMode 更改為“FullRowSelect”。

希望這可以幫助。

確保事件已注冊在此處輸入圖像描述

然后執行事件:

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

您可以在Form的構造函數中注冊此事件。

例如:

public Form1()
{
   InitializeComponents();
   dataGridView1.UserDeletingRow += dataGridView1_UserDeletingRow;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM