简体   繁体   中英

Using one scroll bar to control two DataGridView

我正在尝试控制两个DataGridView,仅其中一个DataGridView垂直滚动条可见。

protected void grid1_Scroll(object sender, ScrollEventArgs e)
{
    grid2.VerticallScrollBar.Value = e.NewValue;
}

If both DataGridView controls have equal number of rows, you can do the following. I am using this to compare two SQL resultsets side by side.

Set Scroll event handlers on both controls.

private void DataGridView1_Scroll(object sender, ScrollEventArgs e)
{
    DataGridView2.FirstDisplayedScrollingRowIndex =
        DataGridView1.FirstDisplayedScrollingRowIndex;
}

private void DataGridView2_Scroll(object sender, ScrollEventArgs e)
{
    DataGridView1.FirstDisplayedScrollingRowIndex =
        DataGridView2.FirstDisplayedScrollingRowIndex;
}

In Form.Load():

Grid1.Scroll += (s, ev) => Grid2.VerticalScrollBar.Value = Grid1.VerticalScrollBar.Value;

Edit: We can't assign Grid2.VerticalScrollingOffset as I had originally suggested, as it's a ReadOnly property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM