Solved

DataGridView - Setting Focus on a Cell

Posted on 2011-09-08
4
973 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
  • 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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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