Sco*_*ttG 27 c# datagrid winforms
在我的C#winforms应用程序中,我有一个数据网格.当datagrid重新加载时,我想将滚动条设置回用户设置的位置.我怎样才能做到这一点?
编辑:我正在使用旧的winforms DataGrid控件,而不是较新的DataGridView
BFr*_*ree 38
您实际上并不直接与滚动条进行交互,而是设置了FirstDisplayedScrollingRowIndex.因此,在重新加载之前,捕获该索引,一旦重新加载,将其重置为该索引.
编辑:评论中的好点.如果你正在使用a DataGridView那么这将有效.如果您使用的是旧版本,DataGrid那么最简单的方法就是继承它.看到这里:联系
DataGrid具有受保护的GridVScrolled方法,可用于将网格滚动到特定行.要使用它,从DataGrid派生一个新网格并添加一个ScrollToRow方法.
C#代码
public void ScrollToRow(int theRow)
{
//
// Expose the protected GridVScrolled method allowing you
// to programmatically scroll the grid to a particular row.
//
if (DataSource != null)
{
GridVScrolled(this, new ScrollEventArgs(ScrollEventType.LargeIncrement, theRow));
}
}
Run Code Online (Sandbox Code Playgroud)
是的,肯定是FirstDisplayedScrollingRowIndex.您需要在一些用户交互后捕获此值,然后在网格重新加载后,您将要将其设置回旧值.
例如,如果通过单击按钮触发重新加载,则在按钮单击处理程序中,您可能希望将第一行作为将该值放入变量的命令:
// Get current user scroll position
int scrollPosition = myGridView.FirstDisplayedScrollingRowIndex;
// Do some work
...
// Rebind the grid and reset scrolling
myGridView.DataBind;
myGridView.FirstDisplayedScrollingRowIndex = scrollPosition;
Run Code Online (Sandbox Code Playgroud)