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

我有一些问题,现在不要如何保持DataGridView的滚动位置。

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

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

 int index = dataGridView1.CurrentRow.Index; /* * Your Refresh Code */ dataGridView1.FirstDisplayedScrollingRowIndex = index; 

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

 int currentIndex= dataGridView1.CurrentRow.Index; 

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

  • 要滚动并设置当前行,请设置CurrentCell
  • 要仅滚动,请设置FirstDisplayedScrollingRowIndex

滚动并设置当前行:

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

滚动:

 dataGridView1.FirstDisplayedScrollingRowIndex = currentIndex;