Using one scroll bar to control two DataGridView

扶醉桌前 提交于 2019-12-20 06:26:03

问题


I am trying to control two DataGridView's with only one of the DataGridView vertical scroll bars being visible.


回答1:


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



回答2:


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;
}



回答3:


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.



来源:https://stackoverflow.com/questions/13229589/using-one-scroll-bar-to-control-two-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!