DataGridView Preserve Selected Index and Scroll Position after update and reload [closed]

与世无争的帅哥 提交于 2019-11-29 15:18:42

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:

Scroll And set current row:

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

Scroll:

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