none
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function

    Question

  • Hi, I want to my this code to works. When a user ends editing a cell I want the grid to automatically move to the next cell. I tried this code:

    Private Sub m_dtgControlTiempos_CellEndEdit(ByVal sender As System.Object, ByVal e As   System.Windows.Forms.DataGridViewCellEventArgs) Handles m_dtgControlTiempos.CellEndEdit

            Dim nextIndex As Integer = Math.Min(m_dtgControlTiempos.ColumnCount - 1, m_dtgControlTiempos.CurrentCell.ColumnIndex + 1)
            m_dtgControlTiempos.CurrentCell = m_dtgControlTiempos.CurrentRow.Cells(nextIndex)
            m_dtgControlTiempos.BeginEdit()
    End Sub

    but it throws me an exception: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function

    If I change the variable nextIndex for a integer literal it works.

    Any solution?

    Wednesday, May 07, 2008 9:59 PM

Answers

  •  

    Hi rassiel,

    The reason why you got reentrant exception is that setting the current cell will force the CellEndEdit event to be triggered. To avoid this exception, please try something like the following:

    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

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Tuesday, May 13, 2008 8:06 AM
  • The cellendedit event is called when the user finishes editing the cell,  Usually a cell comes out of edit mode when the user moves to the next cell.  How are you ending the edit session for the cell? 

    Thursday, May 08, 2008 10:51 AM

All replies

  • The cellendedit event is called when the user finishes editing the cell,  Usually a cell comes out of edit mode when the user moves to the next cell.  How are you ending the edit session for the cell? 

    Thursday, May 08, 2008 10:51 AM
  • so? what do you suggest?
    Monday, May 12, 2008 5:51 AM
  •  

    Hi rassiel,

    The reason why you got reentrant exception is that setting the current cell will force the CellEndEdit event to be triggered. To avoid this exception, please try something like the following:

    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

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Tuesday, May 13, 2008 8:06 AM
  • This positions the cursor in the wrong cell if the user exits the current cell by clicking on another cell, says the cell below the current one.

    Tuesday, December 02, 2008 4:03 PM
  • Thanks - using .BeginInvoke helped me with a similar scenario.

    In my scenario, on the bindingsource position changed event, my code would remove an element from the binding source. If the user would select a new row the new row index would end up being one less then what was selected and the dgView would be out of sync with the bindingsource. If the user would enter anything into the selected cell the dgView would throw a currency manager exception because basically the selected row wasn't binded to anything. To get around this I tried setting the current selected cell to the actual new row and I would get the "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function" exception. Using BeginInvoke solved the problem.
    Thursday, February 11, 2010 1:02 AM
  • Here is the C# version....

            delegate void SetColumnIndex(int i);

            private void myGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                if(myGridView.CurrentCell.ColumnIndex != myGridView.Columns.Count - 1)
                {
                    int nextindex = Math.Min(myGridView.Columns.Count - 1, myGridView.CurrentCell.ColumnIndex + 1);

                    SetColumnIndex method = new SetColumnIndex(Mymethod);

                    myGridView.BeginInvoke(method, nextindex);

                }
            }

            private void Mymethod(int columnIndex)
            {
                myGridView.CurrentCell = myGridView.CurrentRow.Cells[columnIndex];
                myGridView.BeginEdit(true);
            }

    Monday, October 31, 2011 6:05 PM
  • This solve my problem also.

    But what i actually did was copy paste.

    So can you explain what we actually doing?

    What i understand is that we create a sub as a Delegate Sub with the same parameters with the sub we want to use.

    Then we are making a variable as the new Delegate Sub we created

    and at the end we begin invoke by calling this variable and pass the parameter to the procedure.

     

    Ok, but what actually this code does?

    In anyway even if you dont answear i thank you, because i solved my problem, but really i would like to understand this better.

    Tuesday, January 31, 2012 12:05 PM
  • Rong-Chun, Viv189, et al -

    I have run into the same exception. 

    One thing I don't understand about the proposed workaround is why it's helpful/necessary to use the delegate approach.  Since the delegate is being invoked within the CellEndEdit event handler itself (or a method it invokes), why isn't assigning the CurrentCell in the delegate code not the same as assigning it directly in the event handler?  It seems that either approach should trigger the root problem.  What's going on that isn't evident?  Like chris_nikol, I'd like to understand.

    Incidentally, I've found that the workaround can be made more general by:

    1. Declaring a local variable (in C#:  private DataGridViewCell currentCell;) that's accessible in the event handler and the delegate method;

    2. Assigning the desired cell to that variable in the event handler (currentCell = dgv[desiredCol, desiredRow];)

    3. Assignng the local variable to the dgv's CurrentCell property in the delegate method (dgv.CurrentCell = currentCell;)

    I must admit that I'm not sure why all this works (hence my question), but it seems to.  I say seems to because my experience is that I only got the initial exception sometimes.  That is, sometimes I could directly assign dgv.CurrentCell in the event handler without a problem, but not other times.  I've not yet seen a pattern to when it happens versus not.

    Thanks.  Steve





    Sunday, April 08, 2012 2:35 PM