How do i set the currentcell in a Datagridview without error?

Here is the error
" InvalidOperationException
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function."



Here is the code why doesn't this work?

 Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
 
        Dim intYCcord As Integer = 0
        Dim intXRcord As Integer = 0
 
        intYCcord = Me.DataGridView1.CurrentCell.ColumnIndex
        intXRcord = Me.DataGridView1.CurrentCell.RowIndex
 
 
        If Me.DataGridView1.CurrentCellAddress.Y < Me.DataGridView1.ColumnCount - 1 Then
            If Me.DataGridView1(intYCcord, intXRcord).ReadOnly = True Then
 
                Do Until Me.DataGridView1(intYCcord, intXRcord).ReadOnly = False And Me.DataGridView1.ColumnCount - 1
                    intYCcord = intYCcord + 1
                Loop
 
                Me.DataGridView1.CurrentCell = Me.DataGridView1(intYCcord, intXRcord)
 
 
            End If
        End If
End Sub
MvazAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
Can you tell what you want to do ?
0
MvazAuthor Commented:
Checking for the next writable cell and skipping read only cells in the datagridview.
0
Jorge PaulinoIT Pro/DeveloperCommented:
The problem is that the operacion will reprodoce a loop  causing an overflow

Ignore the error in the beginning using

On Error Resume Next
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

MvazAuthor Commented:
do i need a different loop or conditional for this to work?
0
Jorge PaulinoIT Pro/DeveloperCommented:
When you use a CellEnter event to select another cell it will do that event again. I was making some tests and try this method:

    Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        Dim intYCcord As Integer = 0
        Dim intXRcord As Integer = 0

        intYCcord = Me.DataGridView1.CurrentCell.ColumnIndex
        intXRcord = Me.DataGridView1.CurrentCell.RowIndex

        If Me.DataGridView1.CurrentCellAddress.Y < Me.DataGridView1.ColumnCount - 1 Then
            If Me.DataGridView1(intYCcord, intXRcord).ReadOnly = True Then

                SendKeys.Send("{TAB}")
            End If
        End If

    End Sub
0
MvazAuthor Commented:
There is a small issue with using the SendKey and that is why I was trying the set CurrentCell routine.

Sample;
If you setup 6 columns in a grid and set the read only property as shown below;
C1 = Writeable
C2 = Read Only
C3 = Read Only
C4 = Writeable
C5 = Read Only
C6 = Writeable

At run time when you edit the first cell and press the TAB key it will place the Focus in the second column, every time. This is regardless of its Read Only property setting. Every TAB key after that works great.
0
MvazAuthor Commented:
The problem that I explained in my last comment is if I use a flag with previous code.

   If intTab = 9 Then
              intTab = Keys.Tab
        End If

0
Jorge PaulinoIT Pro/DeveloperCommented:
I'm not seeing the problem. Can you post the code that you have now ?
0
MvazAuthor Commented:
How do I post it or do you want me to email it to you?
0
Jorge PaulinoIT Pro/DeveloperCommented:
You can post it on www.ee-stuff.com and then you have to indicate this question id = 23151517

Tell me when it's there.
0
MvazAuthor Commented:
Posted and Thank you for looking at it.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Ok I see the file but now provide me more details how do you like to work. Using mouse, using keys, what columns, etc, etc.
0
MvazAuthor Commented:
When the Tab key is pressed skip all "Read Only" cells.
If the the user Mouse Click on a "Read Only" cell set it to CurrentCell
 
0
Jorge PaulinoIT Pro/DeveloperCommented:
Try with this small changes:

' Change 1
 Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Tab Then
            intTab = 9
        End If
    End Sub

' Change 2
Private Sub NextWriteble()

        Dim intYCcord As Integer = 0
        Dim intXRcord As Integer = 0

        intYCcord = Me.DataGridView1.CurrentCell.ColumnIndex
        intXRcord = Me.DataGridView1.CurrentCell.RowIndex

        If Me.DataGridView1.CurrentCellAddress.Y < Me.DataGridView1.ColumnCount - 1 Then
            If Me.DataGridView1(intYCcord, intXRcord).ReadOnly = True Then
                SendKeys.Send("{TAB}")
            Else
                intTab = 0
            End If
        End If
End Sub
0
MvazAuthor Commented:
To see the problem Run the project and put a 1 in the first Row/Column and het the Tab Key. The Tab will place you in the second Cell and its a Read Only cell.

Change 1
No effect.

Change 2
This option causes the focus to end up on the very next cell regardless of its Read Only property.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Ok, new update:

Add this:
    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        DataGridView1_KeyDown(sender, Nothing)
    End Sub

Change this:
 Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e Is Nothing Then
            intTab = 9
            Exit Sub
        End If
        If e.KeyCode = Keys.Tab Then
            intTab = 9
        End If
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MvazAuthor Commented:
Awesome, you truly are a Guru.
Thank you very much for all your help&&
0
Jorge PaulinoIT Pro/DeveloperCommented:
Glad I could help and thanks for the grade!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.