C# 如何在我的 winforms 应用程序中设置数据网格滚动条的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/378330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:13:32  来源:igfitidea点击:

How can I set the position of my datagrid scrollbar in my winforms app?

c#winformsdatagrid

提问by ScottG

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?

在我的 C# winforms 应用程序中,我有一个数据网格。当数据网格重新加载时,我想将滚动条设置回用户设置的位置。我怎样才能做到这一点?

EDIT: I'm using the old winforms DataGrid control, not the newer DataGridView

编辑:我使用的是旧的 winforms DataGrid 控件,而不是较新的 DataGridView

采纳答案by BFree

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.

您实际上并不直接与滚动条交互,而是将FirstDisplayedScrollingRowIndex. 因此,在重新加载之前,捕获该索引,重新加载后,将其重置为该索引。

EDIT:Good point in the comment. If you're using a DataGridViewthen this will work. If you're using the old DataGridthen the easiest way to do that is to inherit from it. See here: Linkage

编辑:评论中的好点。如果您使用的是 aDataGridView那么这将起作用。如果您使用的是旧的,DataGrid那么最简单的方法就是继承它。见这里:链接

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.

DataGrid 有一个受保护的 GridVScrolled 方法,可用于将网格滚动到特定行。要使用它,请从 DataGrid 派生一个新网格并添加一个 ScrollToRow 方法。

C# code

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));
    }
}

回答by sfuqua

Yep, definitely FirstDisplayedScrollingRowIndex. You'll need to capture this value after some user interaction, and then after the grid reloads you'll want to set it back to the old value.

是的,绝对是FirstDisplayedScrollingRowIndex。您需要在一些用户交互后捕获此值,然后在网格重新加载后,您需要将其设置回旧值。

For instance, if the reload is triggered by the click of a button, then in the button click handler, you might want to have as your first line a command that places this value into a variable:

例如,如果重新加载是由单击按钮触发的,那么在按钮单击处理程序中,您可能希望将命令作为第一行将该值放入变量中:

// Get current user scroll position
int scrollPosition = myGridView.FirstDisplayedScrollingRowIndex;

// Do some work
...

// Rebind the grid and reset scrolling
myGridView.DataBind;
myGridView.FirstDisplayedScrollingRowIndex = scrollPosition;

回答by Bravo

Just posted the answer on the link given by BFree

刚刚在给出的链接上发布了答案 BFree

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 ScrollToRowmethod.

DataGrid 有一个受保护的 GridVScrolled 方法,可用于将网格滚动到特定行。要使用它,请从 DataGrid 派生一个新网格并添加一个ScrollToRow方法。

C# code

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));
    }
}

VB.NET code

VB.NET 代码

Public Sub ScrollToRow(ByVal theRow As Integer)
    '
    ' Expose the protected GridVScrolled method allowing you
    ' to programmatically scroll the grid to a particular row.
    '
    On Error Resume Next

    If Not DataSource Is Nothing Then
        GridVScrolled(Me, New ScrollEventArgs(ScrollEventType.LargeIncrement, theRow))
    End If
End Sub

回答by Thunder

Store your vertical and horizontal scroll values into some variable and reset them.

将您的垂直和水平滚动值存储到某个变量中并重置它们。

int v= dataGridView1.VerticalScrollingOffset ;
int h= dataGridView1.HorizontalScrollingOffset ;
//...reload
dataGridView1.VerticalScrollingOffset = v;
dataGridView1.HorizontalScrollingOffset =h; 

回答by Pollitzer

I used the answer by @BFree, but also needed to capture the first visible row in the DataGrid:

我使用了@BFree 的答案,但还需要捕获以下内容中的第一个可见行DataGrid

int indexOfTopMostRow = HitTest(dataGrid.RowHeaderWidth + 10, 
                                dataGrid.PreferredRowHeight + 10).Row;

回答by GingerBeer

Even though this is an old question, Many of the solutions above did not work for me. What worked ultimately was:

尽管这是一个老问题,但上面的许多解决方案对我都不起作用。最终奏效的是:

if(gridEmployees.FirstDisplayedScrollingRowIndex != -1) gridEmployees.FirstDisplayedScrollingRowIndex = 0;