Hello everyone, I have an unpredicted behavior from a datatable bound datagridview control and perhaps someone can solve the mystery for me. I have a DataGridView control that is set to:
AllowUserToAddRows=False
AllowUserToDeleteRows=False
SelectionMode=FullRowSelect
I have a button that deletes the selected row and the code for the button is below. The objective is to delete the selected row and select the one under the deleted row unless the deleted row was the last row and in that case it should select the previous row which has now become the last row.
It works as expected except when the selected row is the last row and the line
DGV.CurrentCell = DGV(0, DGV.Rows.Count - 1)
is executed. In that case, it does not select the last row and DGV.SelectedRows.Count becomes 0. To be clear, there are many rows in the grid. It just does not select the newly shown last row when the last row is deleted.
I worked around the issue using the second codeblock posted here where I first select the previous row and then delete the row that was selected. It works as expected but I was trying to understand if there is some kind of "refresh" that needs to be done when using "RemoveAt". By the way, DGV.Refresh() and DGV.Update() seem to have little or no effect when used in this case. When I use DGV.Refresh() right after the 'RemoveAt' sometimes it works as expected but very seldom. So seldom that I cannot say with certainty that Refresh() is making any difference. I even tried putting DGV.CurrentCell=DGV(0, DGV.Rows.Count - 1), Refresh, Update and DoEvents() in a loop until the DGV.SelectedRows.Count>0 and I end up in an infinite loop. (See 'What I have tried:')
Non-Working Code
Private Sub BTNDelete_Click(sender As Object, e As EventArgs) Handles BTNDelete.Click
Dim CurrentIndex As Integer = DGV.SelectedRows(0).Index
DGV.Rows.RemoveAt(CurrentIndex)
If CurrentIndex <= DGV.Rows.Count - 1 Then
DGV.CurrentCell = DGV(0, CurrentIndex)
Else
DGV.CurrentCell = DGV(0, DGV.Rows.Count - 1)
End If
End Sub
Working Code
Private Sub BTNDelete_Click(sender As Object, e As EventArgs) Handles BTNDelete.Click
If DGV.SelectedRows.Count > 0 Then
Dim CurrentIndex As Integer = DGV.SelectedRows(0).Index
If CurrentIndex = 0 Then
DGV.CurrentCell = DGV(0, 0)
Else
DGV.CurrentCell = DGV(0, CurrentIndex - 1)
End If
DGV.Rows.RemoveAt(CurrentIndex)
End If
End Sub
What I have tried:
DGV.Rows.RemoveAt(CurrentIndex)
While DGV.SelectedRows.Count = 0
DGV.CurrentCell = DGV(0, DGV.Rows.Count - 1)
DGV.Refresh()
DGV.Update()
Application.DoEvents()
End While