DataGridView Preserve Selected Index and Scroll Position after update and reload

£可爱£侵袭症+ 提交于 2019-12-18 09:03:22

问题


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?


回答1:


Save the row index, do your refresh, then set the FirstDisplayedScrollingRowIndex property.

int index = dataGridView1.CurrentRow.Index;

/*
 * Your Refresh Code
 */

dataGridView1.FirstDisplayedScrollingRowIndex = index;



回答2:


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:

  • To scroll and set the current row, set CurrentCell.
  • To only scroll, set FirstDisplayedScrollingRowIndex.

Scroll And set current row:

this.dataGridView1.CurrentCell =  this.DataGridView1.Rows[currentIndex].Cells[0];

Scroll:

dataGridView1.FirstDisplayedScrollingRowIndex = currentIndex;


来源:https://stackoverflow.com/questions/34776898/datagridview-preserve-selected-index-and-scroll-position-after-update-and-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!