select cell in datagridview

How do you programatically select a cell in a datagridview?
yanci1179Asked:
Who is Participating?
 
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
0
 
Bob LearnedCommented:
Row:
  this.dataGridView1.CurrentRow = index;

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

Bob
0
 
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.

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
0
 
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.  
0
 
yanci1179Author Commented:
thanks the last one worked!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.