Using one scroll bar to control two DataGridView

前端 未结 3 1820
天命终不由人
天命终不由人 2021-01-26 02:39

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

相关标签:
3条回答
  • 2021-01-26 03:16

    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.

    0 讨论(0)
  • 2021-01-26 03:19

    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;
    }
    
    0 讨论(0)
  • 2021-01-26 03:31
    protected void grid1_Scroll(object sender, ScrollEventArgs e)
    {
        grid2.VerticallScrollBar.Value = e.NewValue;
    }
    
    0 讨论(0)
提交回复
热议问题