[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

DataGridView - Setting Focus on a Cell

Posted on 2011-09-08
4
Medium Priority
?
1,001 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’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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