0

I have 2 DataGridViews. Assume that they have always the same amount of rows with the same height. Whenever I move the ScrollBar on one of them (both with the MouseWheel and the ScrollBar), I'd like to move the other one simultaneously (I only want to do this on the vertical ScrollBars). How can I achive this?

  • Why are they not just one datagridview then? – Caius Jard Oct 06 '21 at 09:52
  • 2
    Syncronize their `FirstDisplayedScrollingRowIndex` properties – Caius Jard Oct 06 '21 at 09:53
  • @Caius Jard it was a specific request of the client. What do you mean by syncronizing their properties? –  Oct 06 '21 at 10:03
  • 1
    DGV has a FirstDisplayedScrollingRowIndex property that either tells you or allows you to set which row is displayed first.. If you scroll one DGV (so it's eg now showing row 10 as its first row) you can get which index is first (e.g. 10) and set the other DGV so it's showing the same first row (eg 10) – Caius Jard Oct 06 '21 at 10:22

1 Answers1

0

What @Caius Jard is saying to you to do is:

First, create two DataGridView, then on the Scroll event sync the FirstDisplayedScrollingRowIndex.

dataGridView1.Scroll += DataGridView1OnScroll;

private void DataGridView1OnScroll(object sender, ScrollEventArgs scrollEventArgs) {
    dataGridView2.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
}

Observation

I tried to use WndProc to sync two DataGrids, using this, but it did not work… Well, it worked with the mouse wheel, but not by dragging the scrollbar.

D.Kastier
  • 2,640
  • 3
  • 25
  • 40