Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView control scrolls to top when a property changed

On a Windows Form I have a DataGridView control with records that is filled by a data source (data binding). Each record presents a data object.

Not all the rows are displaying: only the first 10 for example. So the user can scroll down to see the another records. Nothing special about this.

But when a user clicks on a row after scrolling, a data property of the object of row is changing and this refreshes the DataGridViewand - it "scrolls" to top of datagrid (maybe the whole DataGridView is refreshing). This is not desirable.

How can I keep the current scroll position during a record update?

like image 215
robertpnl Avatar asked Mar 01 '11 20:03

robertpnl


1 Answers

You can use the DataGridView's FirstDisplayedScrollingRowIndex property.

It gets/sets the index of the first row displayed on your DGV.

Use it like this:

        int rowIndex = dataGridView.FirstDisplayedScrollingRowIndex;

        // Refresh your DGV.

        dataGridView.FirstDisplayedScrollingRowIndex = rowIndex;

Of course this won't work quite right if sort or add/remove rows to your DGV (you did say you were updating so maybe you're OK).

like image 163
Jay Riggs Avatar answered Oct 17 '22 20:10

Jay Riggs