select cell in datagridview

How do you programatically select a cell in a datagridview?
yanci1179Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Row:
  this.dataGridView1.CurrentRow = index;

Cell:
  this.dataGridView1.CurrentCell = this.dataGridView1[row, column];

Bob
yanci1179Author Commented:
after I validate a cetain cell, I want to tab to the next cell

I tried:

SendKeys.Send("{TAB}")  and the application stalls
 
I tried:

DgvSched.CurrentCell = DgvSched[3, DgvSched.CurrentRow.Index];

and also

DgvSched.CurrentCell = DgvSched[3, e.RowIndex];

and I get the following error:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

when I remove the above statement the error does not come up.

Bob LearnedCommented:
Where are you calling this from?  My guess is the CurrentCellChanged event handler.  If so, then you are going to get an infinite loop.

Bob
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

yanci1179Author Commented:
Yes, why does is go to an infinite loop?  What can I do to select the cell?

I don't know the row number only the column number so this.dataGridView1[row, column];   won't help if I don't know the row number, right?  I do know that the row number is the currentRow, is there a way of getting the index of the currentRow?

I've been trying to get this all day with no luck.  
Bob LearnedCommented:
Let me see if I can explain it this way:

CurrentCellChanged
 Goto (1,1)
  CurrentCellChanged
   Goto(1,2)
    CurrentCellChanged
      Goto(1,3)
        CurrentCellChanged
...

When you change the cell in CurrentCellChanged, it fires CurrentCellChanged again.  One way around that is to define a module-level flag variable:

private bool _isChangingCell = false;

private void CurrentCellChanged(...

   if (!m_isChangingCell)
   {
     _isChangingCell = true;

     this.DgvSched.CurrentCell = DgvSched[3, DgvSched.CurrentRow.Index];
     
     _isChangingCell = false;
   }

}

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yanci1179Author Commented:
thanks the last one worked!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.