Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView: Scrolling to Selected row
 

DataGridView: Scrolling to Selected row

(.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?

Jayson Go - Old Account  Monday, October 24, 2005 8:41 PM
Silly me, I found that there's a DataGridView.FirstDisplayedScrollingRowIndex property that does the job.
Jayson Go - Old Account  Tuesday, October 25, 2005 7:29 PM
Silly me, I found that there's a DataGridView.FirstDisplayedScrollingRowIndex property that does the job.
Jayson Go - Old Account  Tuesday, October 25, 2005 7:29 PM
Thanks I was just looking for the exact same thing.
kennylouie  Monday, July 31, 2006 6:31 PM
So Did I, I think a name like ScrollToStartOfSelection method would have been usefull
BlackZombie  Wednesday, March 07, 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;

}
}


TrevorSB  Friday, December 07, 2007 3:30 PM

Thanks. Good to know. I was sure there had to be something but I was about to give up.

GrantJohnson73  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;
dgItems.Update();

}

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...?
Edmundas Žemaitis  Monday, July 14, 2008 6:46 AM
Thanks. I was just looking for that.
MikeSchoenbauer  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.
Jo-c  Wednesday, September 23, 2009 1:56 AM

You can use google to search for other answers

Custom Search

More Threads

• Print DataGrid C# .Net (vs2003)
• Prevent 'RemoveCurrent' after BindingNavigator DeleteItem Click
• Datagridview KeyDown Event
• How to initialize a value field?
• Databinding question
• Specifying the format for a column bound to an expression column
• Combo Box Items Value
• C# windows reading from datagrid
• Keeping selected rows when sorting a DataGridView?
• VB 2005 closes with error when clicking on "Choose Data Source" on DataGridView