同步两个DataGridViews的水平滚动事件 [英] Sync horizontal scroll event of two DataGridViews

查看:233
本文介绍了同步两个DataGridViews的水平滚动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要同步两个 DataGridView 控件的滚动事件,这样当我水平滚动第一个DGV时,第二个DGV也应该以相同的方式滚动。 / p>

有可能吗?这可以在设计时设置吗?

解决方案

可以按照以下代码完成。您可能正在寻找一种C#的方式。但以下是我在VB.NET应用程序中使用的代码。只需将其转换为C#;)



对于第一个网格写入...

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

如果e.ScrollOrientation = ScrollOrientation.VerticalScroll然后退出Sub
如果Me.DataGridView2.Rows.Count> 0 And Me.DataGridView1.Rows.Count> 0然后
Me.DataGridView2.Horizo​​ntalScrollingOffset = e.NewValue'Me.DataGridView1.Horizo​​ntalScrollingOffset
End If

End Sub

对于第二个网格写...

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

如果e.ScrollOrientation = ScrollOrientation.VerticalScroll然后退出Sub
如果Me.DataGridView1.Rows.Count> 0和Me.DataGridView2.Rows.Count> 0然后
Me.DataGridView1.Horizo​​ntalScrollingOffset = e.NewValue'Me.DataGridView2.Horizo​​ntalScrollingOffset
End If

End Sub

希望有帮助?


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?

解决方案

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?

这篇关于同步两个DataGridViews的水平滚动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆