none
synchronyse scroll bar in datagridview RRS feed

  • Question

  • hi all,

     

    i want to synchronize scroll bar into 2 datagridview (VB.Net)

     

    i can't make a component inherits from datagrid view. i try to send get scroll info and send new event to my component (i've found code in "CodeProject.net" but it doesen't work

     

    code found:

     

    Private Sub Selling_Scrolled(ByVal e As System.Windows.Forms.ScrollEventArgs) Handles Grid1.Scrolled

        Dim posGrid1 As New POINT

        Dim posGrid2 As New POINT

        SendScrollPosMessage(Grid1.Handle, EM_GETSCROLLPOS, New IntPtr(0), posGrid1)

        SendScrollPosMessage(Grid1.Handle(), EM_GETSCROLLPOS, New IntPtr(0), posGrid2)

        posGrid2.x = posGrid1.x

        SendScrollPosMessage(Grid1.Handle(), EM_SETSCROLLPOS, New IntPtr(0), posGrid2)

    End Sub

     

    Private Declare Auto Function SendScrollPosMessage _

        Lib "user32.dll" Alias "SendMessage" ( _

        ByVal hWnd As IntPtr, _

        ByVal Msg As Integer, _

        ByVal wParam As IntPtr, _

        ByRef lParam As Point) As Integer

     

    Private Const WM_USER = &H400

    Private Const EM_GETSCROLLPOS = WM_USER + 221

    Private Const EM_SETSCROLLPOS = WM_USER + 222

     

    Private Structure POINT

        Public x As Integer

        Public y As Integer

    End Structure

     

    Can i use (and correct) this code?

    how can i do?

     

    thank's for your help

    Saturday, July 14, 2007 10:59 PM

Answers

  •  

    Hi Gpg,

     

    To synchronize scroll bar in datagridview, I would prefer use the following lines of code.

    Code Snippet

            private void dataGridView1_Scroll(object sender, ScrollEventArgs e)

            {

                this.dataGridView2.FirstDisplayedScrollingRowIndex = this.dataGridView1.FirstDisplayedScrollingRowIndex;

                this.dataGridView2.HorizontalScrollingOffset = this.dataGridView1.HorizontalScrollingOffset;

            }

     

            private void dataGridView2_Scroll(object sender, ScrollEventArgs e)

            {

                this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView2.FirstDisplayedScrollingRowIndex;

                this.dataGridView1.HorizontalScrollingOffset = this.dataGridView2.HorizontalScrollingOffset;

            }

     

    Hope this helps.

    Regards

    Monday, July 16, 2007 8:20 AM

All replies

  • Move it from BCL forum to WinForm forum for better responses.
    Monday, July 16, 2007 5:46 AM
  •  

    Hi Gpg,

     

    To synchronize scroll bar in datagridview, I would prefer use the following lines of code.

    Code Snippet

            private void dataGridView1_Scroll(object sender, ScrollEventArgs e)

            {

                this.dataGridView2.FirstDisplayedScrollingRowIndex = this.dataGridView1.FirstDisplayedScrollingRowIndex;

                this.dataGridView2.HorizontalScrollingOffset = this.dataGridView1.HorizontalScrollingOffset;

            }

     

            private void dataGridView2_Scroll(object sender, ScrollEventArgs e)

            {

                this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView2.FirstDisplayedScrollingRowIndex;

                this.dataGridView1.HorizontalScrollingOffset = this.dataGridView2.HorizontalScrollingOffset;

            }

     

    Hope this helps.

    Regards

    Monday, July 16, 2007 8:20 AM
  • it's run fine, thank's
    Monday, July 16, 2007 1:11 PM
  • What i have  to do if the row count in the two grids are not equal say one have 180 and another have 300.

     

    Regards,

    Durga Prasad P.V.

    Monday, July 28, 2008 12:07 PM
  • with time, i'm modifying my code and use that:

       

    Code Snippet

    Private Sub purchaseContractGrid_Scrolled(ByVal e As ScrollEventArgs) Handles purchaseContractGrid.Scrolled
            If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then Exit Sub
            If sellingContractGrid.dgvMain.Rows.Count > 0 And purchaseContractGrid.dgvMain.Rows.Count > 0 Then
                sellingContractGrid.dgvMain.HorizontalScrollingOffset = purchaseContractGrid.dgvMain.HorizontalScrollingOffset
            End If
        End Sub

     

    Private Sub sellingContractGrid_Scrolled(ByVal e As ScrollEventArgs) Handles sellingContractGrid.Scrolled
            If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then Exit Sub
            If sellingContractGrid.dgvMain.Rows.Count > 0 And purchaseContractGrid.dgvMain.Rows.Count > 0 Then
                purchaseContractGrid.dgvMain.HorizontalScrollingOffset = sellingContractGrid.dgvMain.HorizontalScrollingOffset
            End If
        End Sub

     

    these two grid hasn't same size and number of column.

     

    it's rune fine

     

    hope that help you

    Monday, July 28, 2008 12:14 PM
  • Hi Gpg,

     

    thanks for your quick reply.

     

    Here we are disabling the Vertical Scroll, that is what the functionality i need. Here i am not limited to two grids, these grids count may vary runtime may even go to 10 also and un-even. So i want one perfect sync mechanism.

     

    Regards,

    Durga Prasad P.V.

     

     

    Monday, July 28, 2008 1:07 PM
  • Hi Durga

     

    in first time, i synchronize V and H scroll bar, but finally, I'm return where V scroll bar launch an event

    here is a easily sample, but you can use it with few grid

     

    warning with more than 2 synchronized scroll, you must using a variable (boolean) for disabling cascade event

    or handles all scroll event in same event handler

     

    good luck

    Monday, July 28, 2008 1:18 PM
  • Hi,

     

    here i am measuring how much rows i have to scroll based on the row count of those grids.

     

    But if i am converting that calculation to decimal and to integer , i am into cascading scroll events, that i am unable to control.

     

    so please help me on this.

     

    Regards,

    Durga Prasad P.V.

     

    Tuesday, July 29, 2008 3:59 AM
  • try disable cascading event like that.

     

    Code Snippet

    Dim listofGrid As New ArrayList

    Dim disableScrollingEvent As Boolean = False

     

    'register all synchronized grid

    Private Sub registerGrid()

    listofGrid.Add(grid1)

    listofGrid.Add(grid2)

    listofGrid.Add(grid3)

    listofGrid.Add(grid4)

    listofGrid.Add(grid5)

    End Sub

     

    'grid event

    Private Sub Grid1_Scrolled(ByVal e As ScrollEventArgs) Handles grid1.Scrolled

    myAutoScroll(e, grid1)

    End Sub

    Private Sub Grid2_Scrolled(ByVal e As ScrollEventArgs) Handles grid2.Scrolled

    myAutoScroll(e, grid2)

    End Sub

    Private Sub Grid3_Scrolled(ByVal e As ScrollEventArgs) Handles grid3.Scrolled

    myAutoScroll(e, grid3)

    End Sub

    Private Sub Grid4_Scrolled(ByVal e As ScrollEventArgs) Handles grid4.Scrolled

    myAutoScroll(e, grid4)

    End Sub

    Private Sub Grid5_Scrolled(ByVal e As ScrollEventArgs) Handles grid5.Scrolled

    myAutoScroll(e, grid5)

    End Sub

     

    'scrolling all grid

    Private Sub myAutoScroll(ByVal e As ScrollEventArgs, ByVal source As DataGridView)

    If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then Exit Sub

    If disableScrollingEvent Then Exit Sub

     

    disableScrollingEvent = True

    For Each grid As DataGridView In listofGrid

    If grid.RowCount = 0 Then Continue For

    grid.HorizontalScrollingOffset = source.HorizontalScrollingOffset

    Next

    disableScrollingEvent = False

    End Sub

     

     

    Tuesday, July 29, 2008 8:24 AM
  • Yes this will work fine thank you very much..
    Tuesday, September 24, 2019 8:46 AM