The Reentrant call struck again. reentrant call to the SetCurrentCellAddressCore function
I've been doing some checking in my DataGridView to find ways that will cause it to throw an error, and I found one that I just can't seem to solve. Clicking in the column header area when the row is either a new row or the only row in the DGV will cause it to throw the error:- "System.InvalidOperationException: 'Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.'" All I did to create the error was to start the program and delete the contents of the cell then clicked on the column header then the rowvalidating code detected an empty cell that is not allowed nulls and then tries to select the cell that is empty. It will even happen if the cell is empty when the program is started and all you do is click on the column header and nothing else. I've tried several different things like DGV1.CurrentCell = Nothing, DGV1.ClearSelection(), I even tried DGV1.EndEdit() and DGV1.Refresh() just in case it was in edit mode, which it shouldn't have been. Nothing worked. As I said it only happens if the row is the only row or a new row, but never happens with any other row. I tried https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/get-and-set-the-current-cell-wf-datagridview-control?view=netframeworkdesktop-4.8 and a few posts on StackOverFlow, VBForums and the MS Developer Network but nothing worked. I believe it is either something to do with both instances being the last row in the DGV or possibly that clicking on the column header started the sorting and it hasn't finished, even though it isn't doing anything and in one instance it only has to sort one row. Does anybody have any ideas to try? IntError is only an indicator that an error was found and IntColError was the column of the current row where it was found.
ElseIf IntError > 0 Then
e.Cancel = True
Dim IntCurRow As Integer = Me.DGV1.CurrentRow.Index
DGV1.ClearSelection()
DGV1.CurrentCell = DGV1.Rows(IntCurRow).Cells(IntErrorCol) <<<<<<< This line causes the error
Else