【问题标题】:How can I detect when the scroll bar reaches the end of a data grid view?如何检测滚动条何时到达数据网格视图的末尾?
【发布时间】:2014-10-22 17:00:28
【问题描述】:

我想检测滚动条何时到达数据网格视图的末尾,以便在发生这种情况时运行函数。

我正在探索Scroll 事件,但没有成功。

谢谢。

【问题讨论】:

  • 您也许可以使用DataGridView 的高度和DataGridView.ScrollingOffset 的值来确定您是否处于最后。
  • 如果DataGridView 处于虚拟模式,您可以控制在内容可见时为其提供内容,以便知道最后一行何时可见。

标签: c# datagridview scroll


【解决方案1】:

这是另一种方法...

private void dataGrid_Scroll(object sender, ScrollEventArgs scrollEventArgs)
{
    if (dataGrid.DisplayedRowCount(false) + 
        dataGrid.FirstDisplayedScrollingRowIndex
        >= dataGrid.RowCount)
    {
        // at bottom
    }
    else
    {
        // not at bottom
    }
}

【讨论】:

    【解决方案2】:

    这是另一种解决方案:

    每次移动滚动条时都会运行滚动事件。根据您的用例,这可能会导致issues 或性能不佳。因此,更好的方法是仅在用户通过处理EndScroll 事件释放滚动条时运行您的检查和功能。

    但是,您必须使用 LINQ 访问 datagridview's ScrollBar 控件并像这样设置事件处理程序:

    using System.Linq;
    public MyFormConstructor()
    {
        InitializeComponent();
        VScrollBar scrollBar = dgv.Controls.OfType<VScrollBar>().First();
        scrollBar.EndScroll += MyEndScrollEventHandler;
    }
    
    private void MyEndScrollEventHandler(object sender, ScrollEventArgs e)
    {
       if (dgv.Rows[dgv.RowCount - 1].Displayed){ // Check whether last row is visible
          //do something
       }
    }
    

    【讨论】:

      【解决方案3】:

      这应该让你接近...把它放在你的 Scroll 事件中,它会告诉你最后一行何时可见:

        int totalHeight = 0;
        foreach (DataGridViewRow row in dataGridView1.Rows)
          totalHeight += row.Height;
      
        if (totalHeight - dataGridView1.Height < dataGridView1.VerticalScrollingOffset)
        {
          //Last row visible
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-02
        • 1970-01-01
        • 2019-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 1970-01-01
        相关资源
        最近更新 更多