How to delete a specific row in DataGridView using C#?

In C#, you can use the Rows.RemoveAt method of the DataGridView to delete a specific row. First, you need to get the index of the row to be deleted, and then call the Rows.RemoveAt method to delete that row. Here is an example code for deleting a specific row:

int rowIndex = 0; // 要删除的行的索引
dataGridView1.Rows.RemoveAt(rowIndex);

You can also use the Rows.Remove method of the DataGridView to delete a specific row if you have the unique identifier of the row. Here is an example code of deleting a row using the unique identifier of the row:

string rowId = "123"; // 要删除的行的唯一标识符
DataGridViewRow row = dataGridView1.Rows
    .Cast<DataGridViewRow>()
    .FirstOrDefault(r => r.Cells["Id"].Value.ToString().Equals(rowId));

if (row != null)
{
    dataGridView1.Rows.Remove(row);
}

In the code above, it is assumed that the unique identifier of the row to be deleted is stored in a cell named “Id”. You need to modify the code’s “Id” according to your specific situation.

bannerAds