DataGridView Scroll Problem
I have 2 DataGridView controls in a form. Both are having 2 columns having same width.
In both DataGridViews width of the columns makes Horizontal Scroll Bar to be appeared while runtime.
What I want is when user scrolls one DataGridView control, the other DataGridView control should also get scrolled horizontally.
I have written code for the same. However, only scroll bar is getting scrolled and content of the other DataGridView are not getting scrolled.
This is the code :
Private Sub dgvSource_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgvSource.Scroll
For Each ctrl As Control In dgvDestination.Controls
If TypeOf ctrl Is HScrollBar Then
CType(ctrl, HScrollBar).Value = e.NewValue
End If
Next
End Sub
Here, there are 2 DataGridViews, dgvSource and dgvDestination. Through dgvSource.Scroll event, I am able to scroll only the scroll bar of the dgvDestination datagridview, but not able to scroll the content of the same.- Edited byParagGhadshi Saturday, October 31, 2009 3:45 AM
Answers
- Hi ParagGhadshi,
Would you please try to set the HorizontalScrollingOffset Property? I think it can do the trick
Hope this helpsPrivate Sub DataGridView1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll DataGridView2.HorizontalScrollingOffset = DataGridView1.HorizontalScrollingOffset End Sub Private Sub DataGridView2_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView2.Scroll DataGridView1.HorizontalScrollingOffset = DataGridView2.HorizontalScrollingOffset End Sub
Regards
Jeff Shan
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer byParagGhadshi Wednesday, November 04, 2009 8:37 AM
All Replies
- that should work or change to: If TypeOf ctrl Is DGV(Datagridview) Then
For Each ctrl As Control In Controls
If TypeOf ctrl Is HScrollBar Then
CType (ctrl, HScrollBar).Value = e.NewValue
End If
Next
End Sub
Don't judge me, just Upgrade me. Thanks! - Hi,
Scroll bar of the second DataGridView is getting scrolled as per scolling scroll bar of the first DataGridView. However, the contents of the second DataGridView are not getting scrolled.
Thanks,
Parag - Hi ParagGhadshi,
Would you please try to set the HorizontalScrollingOffset Property? I think it can do the trick
Hope this helpsPrivate Sub DataGridView1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll DataGridView2.HorizontalScrollingOffset = DataGridView1.HorizontalScrollingOffset End Sub Private Sub DataGridView2_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView2.Scroll DataGridView1.HorizontalScrollingOffset = DataGridView2.HorizontalScrollingOffset End Sub
Regards
Jeff Shan
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer byParagGhadshi Wednesday, November 04, 2009 8:37 AM
- Thanks Jeff. It works.


