簡體   English   中英

在c#中只清除1行DataGridView

[英]Clear only 1 row of DataGridView in c#

有沒有可能的方法來清除 c# 中 DataGridView 中的 1 行? 嵌入此代碼時的原因

dataGridView1.Rows.Clear();

它清除我的數據網格中的所有項目。 我只希望在按鈕觸發時清除一個項目行。

任何幫助將不勝感激

如果您已經擁有該行,則可以刪除這樣的行:

dataGridView1.Rows.Remove(rowToRemove);

或者,您可以像這樣刪除選定的行:

var rowToRemove = dataGridView1.Rows[dataGridView1.SelectedCells.Item(0).RowIndex];
dataGridView1.Rows.Remove(rowToRemove);

或者,像這樣:

 dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

或者,如果要清除所有選定的行:

foreach (var row in dataGridView1.SelectedRows)
{
    dataGridView1.Rows.Remove(row);
}

理想情況下,您希望綁定到數據源(通常通過 ViewModel),然后從源中刪除選定的數據。

您可以嘗試刪除 dgv 上的當前行。

  YourDGVName.Rows.RemoveAt(YourDGVName.CurrentRow.Index);

您還可以每次取選定的行或特定的行號。

暫無
暫無

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

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