DataGridView: Scrolling to Selected row RRS feed

  • Question

  • (.NET 2.0 Framework)
    In the DataGridView control, I have programmatically selected a row that's currently not displayed in the grid.  However, the grid does not seem to scroll to the selected row automatically, and there doesn't seem to be a way to scroll programmatically to any row, let alone the selected row.

    Some details:
    - my DataGridView is running under VirtualMode
    - I first execute DataGridView.ClearSelection() then DataGridView.Rows[index].Selected = true
    - I have traced the rows that aren't displayed and the pertinent properties are:
       DataGridViewRow.Visible = true
       DataGridViewRow.Displayed = false

    I see no functions under DataGridView or DataGridViewRow to force the DataGridView to force a scroll.  Can anyone confirm this?  And is there a work around?

    Monday, October 24, 2005 8:41 PM


  • Silly me, I found that there's a DataGridView.FirstDisplayedScrollingRowIndex property that does the job.
    Tuesday, October 25, 2005 7:29 PM

All replies

  • Silly me, I found that there's a DataGridView.FirstDisplayedScrollingRowIndex property that does the job.
    Tuesday, October 25, 2005 7:29 PM
  • Thanks I was just looking for the exact same thing.
    Monday, July 31, 2006 6:31 PM
  • So Did I, I think a name like ScrollToStartOfSelection method would have been usefull
    Wednesday, March 7, 2007 1:23 AM
  • I wrote a small function that keeps the selected row in a datagridview centered. You can navigate my app with arrow keys, so this is essential. I call this after every key press. Note that if you only wanted the function to scroll when your selected row hit the bounds of your datagridview, remove the "halfWay" addition and subtraction from the if statement. Hope this helps someone. (Hurray for the first block of code I contribute)

    Code Block

    private void scrollGrid()
                int halfWay = (dataGridView.DisplayedRowCount(false)/2);
                if (dataGridView.FirstDisplayedScrollingRowIndex + halfWay > dataGridView.SelectedRows[0].Index ||
                    (dataGridView.FirstDisplayedScrollingRowIndex + dataGridView.DisplayedRowCount(false)-halfWay) <= dataGridView.SelectedRows[0].Index)
                    int targetRow = dataGridView.SelectedRows[0].Index;
                    targetRow = Math.Max(targetRow-halfWay, 0);
                    dataGridView.FirstDisplayedScrollingRowIndex = targetRow;


    Friday, December 7, 2007 3:30 PM
  • Thanks.  Good to know.  I was sure there had to be something but I was about to give up.

    • Proposed as answer by Jan at Cito Wednesday, April 24, 2013 2:59 PM
    Wednesday, May 21, 2008 2:41 PM

  • I made just as You told, but it still doesn't scroll to the row.

    My code looks like this:

     if (dgItems.Rows.Count - 1 >= selectedIndex)
                        dgItems.Rows[selectedIndex].Selected = true;
                        dgItems.FirstDisplayedScrollingRowIndex = selectedIndex;

    It selects the right row but doeasn't scroll t it... Maybe it's a bug of .NET 3.5 because I'm using this version...?
    Monday, July 14, 2008 6:46 AM
  • Thanks. I was just looking for that.
    Tuesday, March 17, 2009 2:44 PM
  • Hi, try like this:

    if (dgItems.Rows.Count - 1 >= selectedIndex)
                        dgItems.FirstDisplayedScrollingRowIndex = selectedIndex;
                        dgItems.Rows[selectedIndex].Selected = true;
                        dgItems.Rows[selectedIndex].Cells[0].Selected = true;                    

    Good look.
    • Proposed as answer by TamerCE Tuesday, November 17, 2009 12:23 PM
    Wednesday, September 23, 2009 1:56 AM
  • Hi,
    I have a similar problem but more complicated.
    i have a datagridview and its multiselection is true.
    I wanna be focused the max of the current selections after i select and delete several rows with a procedure.
    is there any way for selecting the max index of the selected rows.

    Tuesday, November 17, 2009 12:26 PM
  • I'm having the same issue too.  I can get the table to scroll to the desired row, but the vertical scrollbar doesn't update.

    So when the user grabs the scrollbar, the table jumps to a row nowhere near the currently visible rows.  My code looks identical to the positing above (and I'm using 3.5 as well).  Any thoughts?

    By the way, thanks for the prior posts...
    Tuesday, December 22, 2009 4:11 PM
  • Hi VideoEE,
    After you set the selected row by:
    dgItems.FirstDisplayedScrollingRowIndex = selectedIndex;
    you can call the following to set the scroll bar to the desired location:

    Hope it helps.
    Monday, March 8, 2010 12:03 PM
  • Thanks that helped me.
    Thursday, July 8, 2010 4:20 PM
  • I just register only to say THANKS!
    Tuesday, May 31, 2011 11:23 PM
  • I discovered that you can't put this functionality in the New sub, you need to put in in the Me.Load sub or something after that.

    Don DeVeux, Developer

    Thursday, January 15, 2015 11:15 PM
  • Set the Grid.CurrentCell = the cell that you want to scroll to and be displayed.
    Monday, August 14, 2017 10:56 AM