Reputation: 704
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?
Upvotes: 10
Views: 9939
Reputation: 53593
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).
Upvotes: 12