Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync horizontal scroll event of two DataGridViews

I need to synchronize the scroll event of two DataGridView controls, so that when I horizontally scroll the first DGV, second DGV also should be scrolled in the same way.

Is it possible? Could this be set in design time?

like image 820
user3769690 Avatar asked Jun 28 '14 12:06

user3769690


1 Answers

This can be done in code as follows. You may be looking for a C# way of doing it. But following is a code I used in a VB.NET application. Just convert it to C# ;)

For First Grid write...

Private Sub DataGridView1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll

        If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then Exit Sub
        If Me.DataGridView2.Rows.Count > 0 And Me.DataGridView1.Rows.Count > 0 Then
            Me.DataGridView2.HorizontalScrollingOffset = e.NewValue 'Me.DataGridView1.HorizontalScrollingOffset
        End If

End Sub

For Second Grid write...

Private Sub DataGridView2_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView2.Scroll

        If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then Exit Sub
        If Me.DataGridView1.Rows.Count > 0 And Me.DataGridView2.Rows.Count > 0 Then
            Me.DataGridView1.HorizontalScrollingOffset = e.NewValue 'Me.DataGridView2.HorizontalScrollingOffset
        End If

End Sub

Hope this helped?

like image 104
CAD Avatar answered Nov 02 '22 23:11

CAD