简体   繁体   中英

Clear only 1 row of DataGridView in c#

Is there any possible way to clear only 1 row in DataGridView in c#? cause when embedding this code

dataGridView1.Rows.Clear();

It clears all items in my datagrid. I only want one item row to be cleared when triggered by a button.

Any Help would be greatly appreciated

You can remove a row like this if you already have the row:

dataGridView1.Rows.Remove(rowToRemove);

or, you can remove the selected row like this:

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

or, like this:

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

or, if you want to clear all selected rows:

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

ideally though, you would want to bind to a datasource (usually through a ViewModel), and then remove the selected datum from the source.

You can try this to remove the current row on the dgv.

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

You can also take the selected rows or a specific row number each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM