• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 11447
  • Last Modified:

select cell in datagridview

How do you programatically select a cell in a datagridview?
0
yanci1179
Asked:
yanci1179
  • 3
  • 3
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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
 
yanci1179Author Commented:
thanks the last one worked!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now