none
datagridview select cell

    Question

  • okay, this is driving me crazy!!!

    In my datagridview, on the current row, how do I select/set the focus of a particular cell?  I am already on the row I just want to set the focus to the third cell.

     

    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.

     

    Anybody??!!


     

    Wednesday, April 19, 2006 5:54 PM

All replies

  • From top of my head, you do something like:

     

    DataGridView dv;

    dv.CurrentCell = new DataGridViewCell(row_index, col_index);

     

     

    Thursday, April 20, 2006 12:49 PM
  • Hi,

    Is your problem solved??

    Thanks!
    Bhanu.
    Tuesday, May 02, 2006 10:03 PM
  • I found this to work:

    dataGrid.SelectedRows(0).Cells(0).Selected = True

    Monday, May 28, 2007 7:35 PM
  • Set the cell to Selected
    grid.BeginEdit(false);
    Tuesday, November 06, 2007 4:08 PM
  • Try this:

     

    Code Block

    int intIndex = dataGridView1.SelectedRows[0].Index;

     

    dataGridView1.Rows[intIndex].Selected = false;

    dataGridView1.Rows[intIndex].Cells[3].Selected = true;

     

    // Your validation code here

     

    dataGridView1.Rows[intIndex].Cells[3].Selected = false;

    dataGridView1.Rows[intIndex].Cells[4].Selected = true;

     

     

    Regards,

    Fábio

     

    Edit: If you don't have a full row selected, this will not work, if you only have a cell selected use this intIndex declaration:

    Code Block

     

    int intIndex = dataGridView1.SelectedCells[0].RowIndex;

     

     

    Tuesday, November 06, 2007 4:22 PM
  • dgv.CurrentCell = dgv[column,row];

    dgv.CurrentCell.Selected = true;

     

    Note, this will not work in a currentcellchanged, cell enter, cell leave, or any other cell event as it causes a recursive call to an internal datagridview function (something like setcurrentcellcore, or something like that).  This results in that stack overflow you saw.

     

    To work around it, the only thing i came up with was either

    a) trap all the input keys / mouse clicks/ etc that you want to manipulate the cell navigation,

    or

    b) use the following multi-threaded hack:

     

    1)  Add a background worker to your form.

    2)  Use the cell events (cell enter, currentcell changed, etc.) to call the DoWork function on the background worker (this starts on another thread in the threadpool).

    3)  The DoWork function does nothing except sleep for 1 millisecond.

    4)  The WorkCompleted event on the background worker runs in the same thread that called the DoWork function.  This guarantees that the event that started the background worker has completed (this wouldn't work if it were possible to run the same thread on different processors at the same time... so 20 years from now when everyone has 256 core machines and the thread is no longer the smallest schedulable unit, watch out).  Check the state of the datagridview and change the current cell if needed.

     

    I used option b to implement a "skip a read-only column" when navigating in a datagridview.

     

    Good Luck,

    Kent

     

     

     

     

     

    • Proposed as answer by corso9001 Friday, January 30, 2009 2:32 PM
    Thursday, November 29, 2007 3:15 AM
  • Hi kent,

                 I found your solution interesting. Could you post the option (b) code here as i'm also in the same situation.

    Thanks

    Rama

     

    Wednesday, March 19, 2008 9:49 AM
  • Brilliant!

    I was browsing for two days looking for a solution to this topic and no information was found even in MSDN.

    All the proposed solutions out there doens't work because of the StackOverflow Exception.

    Thanks, Kent.

    Daniel

     

     

    Friday, January 30, 2009 2:31 PM
  • Thinking about this topic, I think that the solution Microsoft should provide would be to add a new event (let's say, CellChanging) that would be triggered after CellLeave and before CellEnter events. This way, we would have an event where to change the next cell to be selected while no cell is selected yet. So the CurrentCell changing would not trigger a new CellLeave event...

    Regards!

    Daniel

     

    Friday, January 30, 2009 2:37 PM
  • Hi.

    Alot of too advanced and tricky solutions for something very simple.

    This is how you do it:

    ___________

    'datagrid is what I named the datagridview object.
    'first I select cell (1,2), note that table starts at (0,0) so this will mark the 3rd row 2nd column.

    DataGrid.CurrentCell = DataGrid.Item(1, 2)

    'See if this works by adding some text, a classic way of trial and error for me-try to display output in text or labels.
    'This will add text "15" in our cell.

    DataGrid.CurrentCell.Value = "15"

    DataGrid.CurrentCell = DataGrid.Item(5, 2)

    DataGrid.CurrentCell.Value = "65"

    _____________

    Regards/Daniel (VB6 fan)
    _____________

    Saw some horrific solutions like this:

    3 Sub Main  
    4   Dim p, Grid  
    5  
    6   ' Obtain the grid object  
    7   Set p = Sys.Process ("DataGridSample")   
    8   Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")  
    9  
    10   ' Select the cell in the 6th row and 3nd column  
    11   Set Grid.CurrentCell = GetCell (Grid, 5, 2)  
    12   ' Select the cell in the 3nd row and the "product" column  
    13   Set Grid.CurrentCell = GetCell (Grid, 2, "product")  
    14 End Sub 

    Saturday, March 28, 2009 4:35 PM
  • i got the same problem yanci, and i picked ur codeline -> SendKeys.Send("{TAB}") .

    My application runs pretty good.

    Lot_dataGridView[ColIndex, RowIndex].Selected =

    false;

     

    SendKeys.Send("{TAB}");

     

    DataGridViewCellEventArgs ea = new DataGridViewCellEventArgs(ColIndex, RowIndex);

    Lot_dataGridView_CellEndEdit(Lot_dataGridView, ea);

    Lot_dataGridView[ColIndex + 1, RowIndex].Selected =

    true;

     

    DataGridViewCellCancelEventArgs cea = new DataGridViewCellCancelEventArgs(ColIndex + 1, RowIndex);

    Lot_dataGridView_CellBeginEdit(Lot_dataGridView, cea);

    This is what i have done and its working good for me.

    Tuesday, September 21, 2010 9:31 AM