C # windows form 2 grids with synchronized scrolling

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

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

+3
source share
7 answers

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

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

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

dgv2.FirstDisplayedScrollingRowIndex = dgv1.FirstDisplayedScrollingRowIndex

Of course, if dgv has a different number of rows, you need to avoid IndexOutOfRange exceptions by checking each dgv row count.

+7
source

DataGridViews Panels :

public Form1()
{
    InitializeComponent();
    panel1.Scroll += new ScrollEventHandler(panel1_Scroll);
}

void panel1_Scroll(object sender, ScrollEventArgs e)
{
    panel2.AutoScrollPosition = new Point(0,e.NewValue);
}

, , DataGridView .

+1

HorizontalScrollingOffset ( VerticalScrollingOffset).

this.dataGridViewDataSample.HorizontalScrollingOffset
+1

. . u . .

0
_dataGridViewInput.Scroll += new ScrollEventHandler(_dataGridViewInput_Scroll);
_dataGridViewOutput.Scroll += new ScrollEventHandler(_dataGridViewOutput_Scroll);

void _dataGridViewInput_Scroll(object sender, ScrollEventArgs e)
{
    this._dataGridViewOutput.FirstDisplayedScrollingRowIndex = this._dataGridViewInput.FirstDisplayedScrollingRowIndex;
}

void _dataGridViewOutput_Scroll(object sender, ScrollEventArgs e)
{
    this._dataGridViewInput.FirstDisplayedScrollingRowIndex = this._dataGridViewOutput.FirstDisplayedScrollingRowIndex;
}
-1

All Articles