How to synchronize datagridview scroll with other scroll.

Mansour_Dalir 2,016 Reputation points
2023-07-14T14:30:48.9566667+00:00

hi .

My Target: making the Total Row At the bottom of the datagridview and hide (scroll datagridview) and horizontal adjustment by my own scroll.

if I'm not mistaken, Max: Use Linq Get All Width Columns If Visible

scroll

All events of two scrolls in parallel. thank

I have found an answer so far, but I think there is still a problem.

    Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll
        Dim GetWidths = DataGridView1.Columns.Cast(Of DataGridViewColumn).Where(Function(Vis) Vis.Visible).Select(Function(k) k.Width).Sum()
        GetWidths += DataGridView1.RowHeadersWidth
        HScrollBar1.Maximum =GetWidths
        HScrollBar1.LargeChange = DataGridView1.Width 
        DataGridView1.HorizontalScrollingOffset = e.NewValue '
    End Sub
    Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll
        DataGridView1.HorizontalScrollingOffset = e.NewValue
    End Sub
  Private Sub DataGridView1_Resize(sender As Object, e As EventArgs) Handles DataGridView1.Resize
        HScrollBar1.LargeChange = DataGridView1.Width
   End Sub

Scroll to the left. No problem. But the right side has a problem. It does not coordinate with datagridview scrolling.enter image description here

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,923 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 33,451 Reputation points Microsoft Vendor
    2023-07-17T06:18:43.18+00:00

    Hi @Mansour_Dalir ,

    Please check if the following code helps. Reset the HScrollBar1.Value after dgv size changed.

        Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll
            dgv.HorizontalScrollingOffset = e.NewValue
        End Sub
        Private Sub dgv_Resize(sender As Object, e As EventArgs) Handles dgv.Resize
            Dim totalWidth As Integer = dgv.RowHeadersWidth + 1 + dgv.Columns.Cast(Of DataGridViewColumn)().Sum(Function(column) column.Width)
    
            HScrollBar1.Maximum = totalWidth
            HScrollBar1.LargeChange = dgv.Width
            HScrollBar1.SmallChange = dgv.Columns(0).Width
            HScrollBar1.Value = dgv.HorizontalScrollingOffset
        End Sub
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.