I'm writing a C# .NET Winforms app with a DataGridView that is loaded with data from a List<t>. When a row is deleted from the DataGridView, the default behavior is that the first row becomes the current row. I want to override that by making the row after the deleted row, if there is one, the new current row. Say I have a DataGridView with 5 rows and I want to delete Row #3. Row 4 then becomes the new Row #3, so I'd want the new Row #3 to become the current row. Or, if I delete Row #5, I want Row #4 to become the new current row. I'm using a global integer "currentindex" to set the current row. currentindex changes to the DataGridView's current row index when DataGridView_SelectionChanged event fires...
private void DataGridView_SelectionChanged(object sender, EventArgs e)
{
currentindex = DataGridView.CurrentRow.Index;
}
Here's a block of code I wrote to update currentindex if it falls out of range after deleting a row...
if (currentindex >= DataGridView.RowCount)
currentindex--;
NOTE: The DataGridView is not bound to the List<t>. I tried using a BindingList<t>, but I'll only have the same problem.
What I have tried:
I've tried putting this block of code in DataGridView_RowsRemoved, DataGridView_UserDeletingRow and DataGridView_UserDeletedRow, but Row #1 still ends up becoming the new current row after a row is deleted.