JPProgrammer
JPProgrammer

Reputation: 506

DataGridViews with synchronized scrollbars: Scrollbar position resets on resize

I have two DataGridViews, one on top of the other, in my form with synchronized scrollbars like in this answer. However, the synchronization is only one way as I have hidden the scrollbar of the grid on top, only showing one scrollbar for both grids. The synchronizing works fine while scrolling the scrollbar, but HorizontalScrollingOffset of the top grid (the one with the hidden scrollbar that is scrolled programmatically) is reset to 0 when the grid is resized.

I think it's because the scrollbar is hidden for the top grid, so the scrollbar isn't able to hold a scrolling value, but haven't been able to find a solution/workaround yet. Does anyone know how to prevent the DataGridView with the hidden scrollbar from resetting when the DataGridView is resized?

Upvotes: 0

Views: 708

Answers (1)

Jon
Jon

Reputation: 3255

    private int scrollPosition = 0;

    private void dataGridView_Scroll(object sender, ScrollEventArgs e)
    {
        // While scrolling, keep track of the scroll position
        if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
        {
            this.scrollPosition = e.NewValue;
        }
    }

    private void dataGridView_Resize(object sender, EventArgs e)
    {
        // Re-scroll back to where we were
        dataGridView.HorizontalScrollingOffset = this.scrollPosition;
    }

Upvotes: 1

Related Questions