Sync horizontal scroll event of two DataGridViews

只谈情不闲聊 提交于 2019-12-05 21:45:43

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!