Fleve
Fleve

Reputation: 401

rowsRemoved event handler called once

I am working with a C# Software in Windows Form in which I have a datagridView where the user can delete rows.

I would like to capture the event of deleting rows at once and the problem is that the event handler RowsRemoved is called once per deleted row. In the Msdn stays that e, the event Argument contains the number of rows that have been deleted but when debugging I see that this number is always 1.

What can I do in order to call this event only when the last row of the selected rows has been deleted and with the total of deleted rows?

Thanks a lot for the help!!!

Upvotes: 2

Views: 1202

Answers (2)

Fleve
Fleve

Reputation: 401

When the group of rows is deleted the number of selected rows is 0 or 1 if the row "new row" is selected:

    private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
    {
        MessageBox.Show("Row deleted");
        if ((dataGridView1.SelectedRows.Count==0)
            || ((dataGridView1.SelectedRows.Count == 1)
            &&(dataGridView1.SelectedRows[0].IsNewRow)))
        {
            MessageBox.Show("Set of rows deleted");
        }
    }

Upvotes: 1

MrFox
MrFox

Reputation: 5116

Use logic to detect whether it's the last row. It even deletes per row when you delete a whole block of rows?

void dgv_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
    if (e.RowIndex == (dgv.RowCount - e.RowCount))
    {
        // Do stuff
    }
}

Upvotes: 0

Related Questions