R – Delete selected row from a DataGridView

netwinforms

I have a DataGridView which is bound to a collection of objects.

I want to make a Delete button on a form so a user could delete selected row.

But there is a problem if a new row (the special last row in the grid) is selected.
If I select the new row and then try to click Delete button, the grid loses its focus and changes the selected row! It makes the the last row selected (the one before the new row). So in Delete button handler, there is another row is selected already, not a new row!

How can I solve this problem, other than hiding that special new row (AllowUserToAddRows = false) and making my own "Add" button and code for it?

Best Answer

You could disable the Delete button when an invalid row (i.e. the special new row) is selected