如何在我的winforms应用程序中设置我的datagrid滚动条的位置? [英] How can I set the position of my datagrid scrollbar in my winforms app?
问题描述
编辑:我使用旧的winform DataGrid控件,而不是较新的DataGridView
您实际上并不直接与滚动条进行交互,而是设置了 FirstDisplayedScrollingRowIndex
。所以在它重新加载之前,捕获该索引,一旦它被重新加载,重置它到该索引。
编辑:评论的好点。如果您正在使用 DataGridView
,那么这将工作。如果您使用旧的 DataGrid
,那么最简单的方法就是继承它。请参阅:链接
DataGrid具有受保护的GridVScrolled方法,可用于将网格滚动到特定行。要使用它,从DataGrid导出一个新的网格并添加一个ScrollToRow方法。
C#代码
public void ScrollToRow(int theRow)
{
//
//暴露受保护的GridVScrolled方法,允许
//以编程方式将网格滚动到特定行。
//
if(DataSource!= null)
{
GridVScrolled(this,new ScrollEventArgs(ScrollEventType.LargeIncrement,theRow));
}
}
In my C# winforms app, I have a datagrid. When the datagrid reloads, I want to set the scrollbar back to where the user had it set. How can I do this?
EDIT: I'm using the old winforms DataGrid control, not the newer DataGridView
You don't actually interact directly with the scrollbar, rather you set the FirstDisplayedScrollingRowIndex
. So before it reloads, capture that index, once it's reloaded, reset it to that index.
EDIT: Good point in the comment. If you're using a DataGridView
then this will work. If you're using the old DataGrid
then the easiest way to do that is to inherit from it. See here: Linkage
The DataGrid has a protected GridVScrolled method that can be used to scroll the grid to a specific row. To use it, derive a new grid from the DataGrid and add a ScrollToRow method.
C# code
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));
}
}
这篇关于如何在我的winforms应用程序中设置我的datagrid滚动条的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!