I have some problem, don't now how to preserve the scroll position in a DataGridView
.
I have over 1000+ rows and scrolling back to edited row is painful. How can I preserve the scroll position and scroll to the edited row after refreshing data?
I have some problem, don't now how to preserve the scroll position in a DataGridView
.
I have over 1000+ rows and scrolling back to edited row is painful. How can I preserve the scroll position and scroll to the edited row after refreshing data?
Save the row index, do your refresh, then set the FirstDisplayedScrollingRowIndex
property.
int index = dataGridView1.CurrentRow.Index;
/*
* Your Refresh Code
*/
dataGridView1.FirstDisplayedScrollingRowIndex = index;
You can get the current row index before reloading data:
int currentIndex= dataGridView1.CurrentRow.Index;
Then after reloading data, you can use either of these options:
CurrentCell
.FirstDisplayedScrollingRowIndex
.Scroll And set current row:
this.dataGridView1.CurrentCell = this.DataGridView1.Rows[currentIndex].Cells[0];
Scroll:
dataGridView1.FirstDisplayedScrollingRowIndex = currentIndex;
void SomeMethod()
{
if (dataGridView != null &&
dataGridView.CurrentRow != null)
this.Invoke(new Action(GetScrollingIndex));
UpdateTable();
if (dataGridView != null &&
dataGridView.CurrentRow != null)
this.Invoke(new Action(SetScrollingIndex));
}
void GetScrollingIndex()
{
scrollingIndex = dataGridView.FirstDisplayedCell.RowIndex;
}
void SetScrollingIndex()
{
dataGridView.FirstDisplayedScrollingRowIndex = scrollingIndex;
}