Solved

DataGridView - Setting Focus on a Cell

Posted on 2011-09-08
4
989 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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