【发布时间】:2016-01-13 20:59:01
【问题描述】:
我有一些问题,现在不知道如何在DataGridView 中保留滚动位置。
我有超过 1000 行,滚动回编辑的行很痛苦。刷新数据后如何保留滚动位置并滚动到已编辑的行?
【问题讨论】:
标签: c# winforms datagridview datagridviewrow
我有一些问题,现在不知道如何在DataGridView 中保留滚动位置。
我有超过 1000 行,滚动回编辑的行很痛苦。刷新数据后如何保留滚动位置并滚动到已编辑的行?
【问题讨论】:
标签: c# winforms datagridview datagridviewrow
保存行索引,进行刷新,然后设置FirstDisplayedScrollingRowIndex 属性。
int index = dataGridView1.CurrentRow.Index;
/*
* Your Refresh Code
*/
dataGridView1.FirstDisplayedScrollingRowIndex = index;
【讨论】:
dataGridView1.SuspendLayout() 和 .ResumeLayout() 中。如果不是这样,也许其他 to suspend the grid 会起作用?
可以在重新加载数据前获取当前行索引:
int currentIndex= dataGridView1.CurrentRow.Index;
然后在重新加载数据后,您可以使用以下任一选项:
CurrentCell。FirstDisplayedScrollingRowIndex。滚动并设置当前行:
this.dataGridView1.CurrentCell = this.DataGridView1.Rows[currentIndex].Cells[0];
滚动:
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;
}
【讨论】: