?
Solved

DataGridView - Setting Focus on a Cell

Posted on 2011-09-08
4
Medium Priority
?
993 Views
Last Modified: 2012-05-12
VB 2010 (Professional)

I'm using an unbound DataGridView for data entry.  The first column is the row number, and will automatically be set when data is first entered in a row.

To that end, I set the first column to ReadOnly.  However, when tabbing from column to column, column 0 still gets focus (nothing can be typed in, though, which is right).  What I would like is for the tabbing to skip over column 0 and go directly to column 1, ready for user input.

So I added code to the CellEnter event as such:
Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
    If e.ColumnIndex = 0 Then
        'DataGridView1.Rows(e.RowIndex).Cells(1).Selected = True 'Didn't Work
        DataGridView1.CurrentCell = DataGridView1.Item(1, e.RowIndex) 'Didn't Work too
    End If
End Sub

Open in new window

As you can see in the sample code, I tried two different methods, but neither works.  Both throw the error:
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

Open in new window


So, how do I do what I want to do?
0
Comment
Question by:Clif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:eryk_s
ID: 36505246
You cant do it in this way :
add private variable for example :

Private cellEnterInUse as boolean=false

Open in new window


and in your sub add one condition :

 
Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
  if not cellEnterInUse Then
   cellEnterInUse=True
    If e.ColumnIndex = 0 Then
        'DataGridView1.Rows(e.RowIndex).Cells(1).Selected = True         
        DataGridView1.CurrentCell = DataGridView1.Item(1, e.RowIndex) 
    End If
  cellEnterInUse=false
  End If
End Sub

Open in new window

0
 
LVL 10

Author Comment

by:Clif
ID: 36505276
No Joy.

Did it work for you?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36506058
0
 
LVL 10

Author Closing Comment

by:Clif
ID: 36510079
It's a little more involved than I really wanted, but I trust Linda Liu (I've received help on several occassions from her on on the MSDN site), so I'm sure the solution would have worked had I implemented it.

Thanks.

On a side note, it is frustrating to know that (according to the posts in that thread) the team in charge of the grid control knows of the issue, and must be aware of the fix(es) posted by their collegue, but have not implemented the fixes in the grid itself.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question