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

datagridview keep scroll position
datagridview scrollbar
c# datagridview scroll to selected row
horizontalscrollingoffset

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

Datagrid, 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  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. So before it reloads, capture that index, once it's reloaded, reset it to that index.

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.

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;

Scrolling in Windows Forms Grid Control control, NET developers about Datagrid in Syncfusion WinForms components: How can I Design Xamarin Apps with 100+ XAML Pages and Templates - Thursday, May 7, 10 A.M. EDT In your handler, if the scrollbar is not visible, size it and position it, and then show it. If it is present, you would have to adjust the sizing code. ? EDIT: I'm using the old winforms DataGrid control, not the newer DataGridView. Just posted the answer on the link given by 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 ScrollToRow method. C# code.

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; 

Getting DataGrid.ScollIntoView to position row at top of view area , Learn about Scrolling support in Syncfusion Windows Forms Grid Control control and To optimize the pixel scrolling set create the derived GridControl and override a RowHeights[0]; } //Gets the row and pixel Delta to the scroll position of the row for It can be drag and drop to the application from the Toolbox window. To set the horizontal scrollbar position to the left, you can use the client-side SetHorizontalScrollPosition method. Handle the client-side BeginCallback event handler and determine if the editing action is calling a callback request using the command property and save this information to a variable:

Just posted the answer on the link given by 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 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));
    }
}

VB.NET code

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

scroll bar position after refreshing grid in UI for WinForms GridView , For scrolling a row into view and also setting it as the top/first visible row, you need to use Attached is a sample application for your reference. Derive a DataGrid. In your derived grid, add a handler for the VertScrollBar.VisibleChanged event. In your handler, if the scrollbar is not visible, size it and position it, and then show it. The code below assumes no horizontal scrollbar is necessary. If it is present, you would have to adjust the sizing code.

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

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

C# DataGridView & Scrollbar position setting problem, My requirement is when i refresh the grid then the selectd row should be selected and the scroll bar Then it sets the scroll-bar position in the initial state. retrieve the sorted view and/or the correct position row after refreshing the data grid. I built a small app. based on previous data in these post. The winform has a table layout split into 4 rows, 1 column. Into the rows I place panels for placing other controls except the DataGrid row which is in the last row. The DataGrid is set with Dock to fill. The form also has a Status bar at the bottom, for future use. What I found is that the status bar blocked the scrollbar as mentioned previously.

ScrollableControl.AutoScroll Property (System.Windows.Forms , i want to search my data programmatically and then jump to the row in the datagrid. My data will almost allways go out of the datagrids bounds so 

VS 2017 Keep scrolling position on DataGridView after update , Gets or sets a value indicating whether the container enables the user to scroll to When you click the button, you will see a horizontal scroll bar appear that will If you position a part of the text box below the bottom of the panel, you will see a from Panel (such as FlowLayoutPanel or TableLayoutPanel) - on your form.

C# Windows Forms, I would like for the scroll position of the DataGridView to be returned to I tried the following code and it is an improvement over setting the For my test I display the dgv, change one field in one record (via a (sponsored); → Unleash Your DevOps Strategy by Synchronizing App and Database Changes

Comments
  • I'm using the old DataGrid control
  • Thank link was perfect. Thanks!
  • I am still using old DataGrid :) , the link answers it perfect. but i am afraid, the link won't last long, since its already more than 4 years old, i am reposting the answer here for future reference.
  • Those who need to know the index of the first visible DataGrid row: Please see my post below.
  • HorizontalScrollingOffset and VerticalScrollingOffset has no setter
  • Works nevertheless (.NET 4.5.1 )
  • VerticalScrollingOffset is read-only (VB.NET 4.5.2) but HorizontalScrollingOffset was all I need and this worked perfectly.