Search code examples
c#winformsdatagridviewscrollbarpanel

DataGridView in a Panel Scroll Resetting


My problem is exactly the same as:

Problem with DataGridView and scroll position

Every time I edit a cell in a DataGridView the parent panel resets its scroll bars. This also happens on when the selection in the DataGridView changes. I, however, need to keep the panel as I have two DataGridViews in it and want to be able to scroll through them both.

I have managed to alleviate some of the problem by listening to the Scroll event of the panel, keeping track of the values of the scrollbars, and reapplying these values in the SelectionChange and CellEndEdit events on the DataGridViews, however this doesn't feel like a very good solution and it makes the screen jitter as the scroll changes to 0 and then back to the value that I set it too. Can anyone point me in a better direction, or know of a way to stop the Panel from resetting its scroll bars?


Solution

  • I found a workaround that works here:

    http://nickstips.wordpress.com/2010/03/03/c-panel-resets-scroll-position-after-focus-is-lost-and-regained/

    By overriding ScrollToControl and returning this.DisplayRectangle.Location it fixed the scroll issue I was having.

    protected override Point ScrollToControl(Control activeControl)
    {
        return this.DisplayRectangle.Location;
    }