Reputation: 10405
I got a DataGridView which just allows user to delete rows (with no direct editing or adding on DataGridView). I will update my database information whenever user deletes rows on DataGridView (there is no binding among DB and gridview).
I have no problem to have deleting row when user is deleting only one row using DeletingRow event like below:
private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
DataGridViewRow row = e.Row; //only single row
}
However i could not capture list of all rows when user selects multiple rows and delete them.
Above event only called once for first row only. In order to handle this case i did set dataGridView1.MultiSelect = false;
however this is a workaround!
So how can i have list of all selected rows which user is deleting, currently?
Upvotes: 1
Views: 2019
Reputation: 10456
All selected rows will be available at dataGridView1.SelectedRows
so you can do something like:
void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
{
this.HandleRowDeletion(row);
}
}
Upvotes: 2
Reputation: 542
Use dataGridView1.SelectedRows
to get a collection (DataGridViewRowCollection
type) that contains all the rows selected. Same works for columns (dataGridView1.SelectedColumns
) and cells (dataGridView1.SelectedCells
).
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedrows.aspx
Upvotes: 1