私のC#winformsアプリには、データグリッドがあります。データグリッドがリロードされたら、スクロールバーをユーザーが設定した場所に戻したいと思います。これどうやってするの?
編集:新しいDataGridViewではなく、古いwinformsDataGridコントロールを使用しています
実際にはスクロールバーを直接操作するのではなく、を設定し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));
}
}
ええ、間違いなくFirstDisplayedScrollingRowIndexです。ユーザー操作の後、この値を取得する必要があります。グリッドがリロードされた後、古い値に戻す必要があります。
たとえば、リロードがボタンのクリックによってトリガーされる場合、ボタン クリック ハンドラーで、この値を変数に配置するコマンドを最初の行として使用することができます。
// Get current user scroll position
int scrollPosition = myGridView.FirstDisplayedScrollingRowIndex;
// Do some work
...
// Rebind the grid and reset scrolling
myGridView.DataBind;
myGridView.FirstDisplayedScrollingRowIndex = scrollPosition;
垂直および水平スクロールの値をいくつかの変数に保存し、それらをリセットします。
int v= dataGridView1.VerticalScrollingOffset ;
int h= dataGridView1.HorizontalScrollingOffset ;
//...reload
dataGridView1.VerticalScrollingOffset = v;
dataGridView1.HorizontalScrollingOffset =h;
によって与えられたリンクに答えを投稿しました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