Solved

DataGridView - Setting Focus on a Cell

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now