28

私のC#winformsアプリには、データグリッドがあります。データグリッドがリロードされたら、スクロールバーをユーザーが設定した場所に戻したいと思います。これどうやってするの?

編集:新しいDataGridViewではなく、古いwinformsDataGridコントロールを使用しています

4

7 に答える 7

39

実際にはスクロールバーを直接操作するのではなく、を設定しFirstDisplayedScrollingRowIndexます。したがって、リロードする前に、そのインデックスをキャプチャし、リロードしたら、そのインデックスにリセットします。

編集:コメントの良い点。を使用している場合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));
    }
}
于 2008-12-18T17:14:58.910 に答える
12

ええ、間違いなくFirstDisplayedScrollingRowIndexです。ユーザー操作の後、この値を取得する必要があります。グリッドがリロードされた後、古い値に戻す必要があります。

たとえば、リロードがボタンのクリックによってトリガーされる場合、ボタン クリック ハンドラーで、この値を変数に配置するコマンドを最初の行として使用することができます。

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

// Do some work
...

// Rebind the grid and reset scrolling
myGridView.DataBind;
myGridView.FirstDisplayedScrollingRowIndex = scrollPosition;
于 2008-12-18T17:46:53.917 に答える
3

垂直および水平スクロールの値をいくつかの変数に保存し、それらをリセットします。

int v= dataGridView1.VerticalScrollingOffset ;
int h= dataGridView1.HorizontalScrollingOffset ;
//...reload
dataGridView1.VerticalScrollingOffset = v;
dataGridView1.HorizontalScrollingOffset =h; 
于 2013-04-29T13:45:28.803 に答える
1

によって与えられたリンクに答えを投稿しましたBFree

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

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
于 2012-07-23T02:18:53.047 に答える