Visual Basic > Visual Basic Forums > Visual Basic Language > DataGridView Scroll Problem
Ask a questionAsk a question
 

AnswerDataGridView Scroll Problem

  • Friday, October 30, 2009 12:52 PMParagGhadshi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

Answers

  • Wednesday, November 04, 2009 3:24 AMJeff ShanMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi ParagGhadshi,

    Would you please try to set the HorizontalScrollingOffset Property? I think it can do the trick
        Private 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
    
    Hope this helps

    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

  • Friday, October 30, 2009 3:25 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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!
  • Saturday, October 31, 2009 3:44 AMParagGhadshi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Wednesday, November 04, 2009 3:24 AMJeff ShanMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi ParagGhadshi,

    Would you please try to set the HorizontalScrollingOffset Property? I think it can do the trick
        Private 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
    
    Hope this helps

    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
    •  
  • Wednesday, November 04, 2009 8:38 AMParagGhadshi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Jeff.  It works.