如何在我的winforms应用程序中设置我的datagrid滚动条的位置? [英] How can I set the position of my datagrid scrollbar in my winforms app?

查看:114
本文介绍了如何在我的winforms应用程序中设置我的datagrid滚动条的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#winforms应用程序中,我有一个datagrid。当datagrid重新加载时,我想将滚动条设置回用户设置的位置。我如何做到这一点?



编辑:我使用旧的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆