Search code examples
c#winformsdatagridview

C# windows form 2 gridviews with synced scroll


I am developing an application in which two datagridviews are being populated from different data sources. I would like to have a single vertical scroll-bar that will make both gridviews work at the same time (scroll up and down together)

can anyone tell me how or direct me to a good tutorial.


Solution

  • If you have dgv1 and dgv2, you can create something like

    dgv1.Scroll += new System.Windows.Forms.ScrollEventHandler(dgv1_Scroll);
    

    Then, in dgv1_Scroll method, you can use FirstDisplayedScrollingRowIndex property:

    dgv2.FirstDisplayedScrollingRowIndex = dgv1.FirstDisplayedScrollingRowIndex
    

    Of course, if dgv's have different ammount of rows, you need to avoid IndexOutOfRange exception by checking each dgv rows count.