Home Dashboard Directory Help
Search

DatagridView control throws InvalidOperationException (reentrant call to SetCurrentCellAddressCore) by Andrew Stanford


Status: 

Closed
 as By Design Help for as By Design


4
0
Sign in
to vote
Type: Bug
ID: 390918
Opened: 1/5/2009 6:03:26 AM
Access Restriction: Public
1
Workaround(s)
view
0
User(s) can reproduce this bug

Description

My requirement is to have the grid navigate to the first cell when a new row/record is added. I have tried to do this in several places in code. The most logical place would be the "RowEnter" event of the datagridview control, but I have also tried binding source "POsitionChanged", Binding Source "AddNew", DatagridView "RowLeave" and "EditControlShowing" events with the same result. This would seem to be a fairly common requirement for data entry... so how do I do it?
Details
Sign in to post a comment.
Posted by atverweij on 5/10/2011 at 5:02 AM
I have the same error in a non editable grid. Just by navigating through the grid using the arrow keys this error occurs. And this error only occurs when no debugger is attached.

Because I use my own grid implementation (inherited from datagridview), I was able to solve this bug that was added "by design" according to Microsoft.

According to the MSDN documentation, the function SetCurrentCellAddressCore returns "True if the current cell was successfully set; otherwise, false".
When this error occurs, the current cell was not set, so this function should return False.

If you are also using an inheritance of datagridview, just add the following code:

Protected Overrides Function SetCurrentCellAddressCore(columnIndex As Integer, rowIndex As Integer, setAnchorCellAddress As Boolean, validateCurrentCell As Boolean, throughMouseClick As Boolean) As Boolean
    Try
        Return MyBase.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick)
    Catch
        Return False
    End Try
End Function

This will solve the problem completely, and has no (not that I found so far) effect on editable grids.
Posted by nothingness111 on 2/26/2010 at 2:56 AM
If you are getting an error when trying to manipulate the cell, use the following workaround...

Code Snippet

    Delegate Sub SetColumnIndex(ByVal i As Integer)



    Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

        If Me.dataGridView1.CurrentCell.ColumnIndex <> Me.dataGridView1.Columns.Count - 1 Then

            Dim nextindex As Integer = Math.Min(Me.dataGridView1.Columns.Count - 1, Me.dataGridView1.CurrentCell.ColumnIndex + 1)

            Dim method As New SetColumnIndex(AddressOf Mymethod)

            Me.dataGridView1.BeginInvoke(method, nextindex)

        End If

    End Sub



    Private Sub Mymethod(ByVal columnIndex As Integer)

        Me.dataGridView1.CurrentCell = Me.dataGridView1.CurrentRow.Cells(columnIndex)

        Me.dataGridView1.BeginEdit(True)

    End Sub


'--------------------------
'Use the "MyMethod" sub to manipulate your cell. Deletion of rows and cells can also occur in this sub.


Regards
Sahil Ebrahim
Posted by Microsoft on 4/27/2009 at 11:40 PM
Thanks for your feedback on the .NET Framework!

The issue that you have reported is actually By Design. The workaround is to use DataGridView.BeginInvoke to set CurrentCell.

Many customers have found it useful to discuss issues like this in the forums (http://forums.microsoft.com/msdn/default.aspx?siteid=1) where Microsoft and other members of the community can recommended ways to achieve the behavior you are aiming for.

Thanks,
UIFx Team
Posted by Microsoft on 1/7/2009 at 10:54 PM
Thank you for the update.

We'll spend some time to investigate this issue further.

Since we are currently busy to lock down the new bits for next release, it takes a while for us to revisit this issue. Thank you for your patience. Will update you when any progress is made.

Regards,
UIFx Team
Posted by Andrew Stanford on 1/7/2009 at 12:35 AM
I tried adding this code to the RowsAdded event, but it doesn;t seem to fire until too late. If I just arrow down from the 3rd column into the empty row at the bottom of the grid the RowsAdded event doesn;t seem to fire until I actually type something and when I do it is still positioned in the 3rd column still. I tried setting the EditMode to "on enter" to see if it would fire the RowsAdded event but this didn't work either. The timer method seems a bit messy. Where would you start the timer and how long would set the interval to?

BTW... I see loads of people are struggling with this out in the world and no one seems to have a good answer. It would seem that the SetCurrentCellAddressCore function has a problem.

Thanks,
Andrew
Posted by Microsoft on 1/6/2009 at 10:00 PM
Thanks for your feedback on the .NET Framework!

You could either use "RowsAdded" event or create a delyed timer to handle the requirment of moving the currency to the first row when a new row/record is added.

Thanks!
UIFx Team
Posted by Microsoft on 1/5/2009 at 11:03 PM
Thanks for your feedback. We are escalating this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Posted by David A Nelson on 1/5/2009 at 2:01 PM
Connect is for reporting known bugs. The best place to ask questions like this is on the MSDN forums at http://forums.microsoft.com/msdn/default.aspx?siteid=1. You will get a much faster response.
Sign in to post a workaround.
Posted by nothingness111 on 2/26/2010 at 2:55 AM
If you are getting an error when trying to manipulate the cell, use the following workaround...

Code Snippet

    Delegate Sub SetColumnIndex(ByVal i As Integer)



    Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

        If Me.dataGridView1.CurrentCell.ColumnIndex <> Me.dataGridView1.Columns.Count - 1 Then

            Dim nextindex As Integer = Math.Min(Me.dataGridView1.Columns.Count - 1, Me.dataGridView1.CurrentCell.ColumnIndex + 1)

            Dim method As New SetColumnIndex(AddressOf Mymethod)

            Me.dataGridView1.BeginInvoke(method, nextindex)

        End If

    End Sub



    Private Sub Mymethod(ByVal columnIndex As Integer)

        Me.dataGridView1.CurrentCell = Me.dataGridView1.CurrentRow.Cells(columnIndex)

        Me.dataGridView1.BeginEdit(True)

    End Sub


'--------------------------
'Use the "MyMethod" sub to manipulate your cell. Deletion of rows and cells can also occur in this sub.


Regards
Sahil Ebrahim