views:

574

answers:

2

As a question similar to this question, I also have an application with a DataGridView on it. I would like to position the rows such that a specific row is at the bottom of the visible part of the list.

This is in response to a button click that moves a row down by one. I want to maintain the selection on the row I'm moving (I already have this part working). If there are lots of rows, the selected row might move below the visible area. I want to scroll down until it's at the bottom of the visible area.

There does not appear to be a LastDisplayedScrollingRowIndex companion to FirstDisplayedScrollingRowIndex.

Any ideas? Thanks.

+1  A: 

As my own guess, I think I need to use FirstDisplayedScrollingRowIndex and the number of rows visible in the DataGridView to calculate the new FirstDisplayedScrollingRowIndex. Maybe I just need to find out what the NumberOfVisibleRows property is called?

Found it. DisplayedRowCount:

if (dataGridView.FirstDisplayedScrollingRowIndex + dataGridView.DisplayedRowCount(false) <= selectedRowIndex)
{
    dataGridView.FirstDisplayedScrollingRowIndex =
        selectedRowIndex - dataGridView.DisplayedRowCount(false) + 1;
}

Code tested and working in my own project.

Chuck Wilbur
+1  A: 

The DisplayedRowCount method will tell you how many rows are displayed on screen. Set the parameter value to true to include partial rows.

var displayedRows = myDataGridView.DisplayedRowCount(false);
Meta-Knight
Thanks. You get an upvote (even though I found it myself before I saw your answer) but I'm going to set my own answer as accepted since it has a working code sample.
Chuck Wilbur
I was about to elaborate my answer but I saw that you found the solution yourself just after I posted this ;-)
Meta-Knight