DataGridView虽然有VerticalScrollBar属性, 但却是受保护的对象, 无法外部访问, 看了一下DataGridView的各项属性, 发现FirstDisplayedScrollingRowIndex就是滚动条的Value, DataGridView的行高乘以FirstDisplayedScrollingRowIndex就是客户区高度.

      以下是有关垂直滚动条的示例:

        int _ScrollValue = 0;

        private void dgvVehicles_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                _ScrollValue = e.NewValue;
            }
        }

        private void BindData()
        {
              // 设置数据源
              ...
              if (dgvVehicles.Rows.Count > _ScrollValue)
                  dgvVehicles.FirstDisplayedScrollingRowIndex = _ScrollValue;
        }

      另有一种做法是设置CurrentCell, 设置该值会使DataGridView跳到该单元格所在行. 方法就是遍历各行找出那个具有唯一性的Cell, 设为当前单元格.

相关文章:

  • 2022-03-06
  • 2021-10-18
  • 2021-12-10
  • 2021-12-19
  • 2021-12-09
猜你喜欢
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2021-08-02
  • 2022-01-08
  • 2022-02-25
相关资源
相似解决方案