DataGridView在更新和重新加载后保留选定的索引和滚动位置

sok*_*kol 3 c# datagridview winforms datagridviewrow

我有一些问题,现在不要如何保存一个滚动位置DataGridView.

我有超过1000行,滚动回编辑行是痛苦的.如何在刷新数据后保留滚动位置并滚动到已编辑的行?

OhB*_*ise 6

保存行索引,进行刷新,然后设置FirstDisplayedScrollingRowIndex属性.

int index = dataGridView1.CurrentRow.Index;

/*
 * Your Refresh Code
 */

dataGridView1.FirstDisplayedScrollingRowIndex = index;
Run Code Online (Sandbox Code Playgroud)

  • @NiravParsana我目前无法测试这个,但也许尝试将此代码包装在`dataGridView1.SuspendLayout()`和`.ResumeLayout()`中。如果不是这样,也许其他[暂停网格](/sf/ask/325499681/)会起作用? (2认同)

Rez*_*aei 5

您可以在重新加载数据之前获取当前行索引:

int currentIndex= dataGridView1.CurrentRow.Index;
Run Code Online (Sandbox Code Playgroud)

然后在重新加载数据后,您可以使用以下任一选项:

滚动并设置当前行:

this.dataGridView1.CurrentCell =  this.DataGridView1.Rows[currentIndex].Cells[0];
Run Code Online (Sandbox Code Playgroud)

滚动:

dataGridView1.FirstDisplayedScrollingRowIndex = currentIndex;
Run Code Online (Sandbox Code Playgroud)