How can I detect when the scroll bar reaches the e

2019-07-21 02:57发布

问题:

I would like to detect when the scroll bar reaches the end of a data grid view, so I can run a function when this happens.

I was exploring the Scroll event, but without success.

Thanks.

回答1:

This should get you close... place this in your Scroll event and it will tell you when the last row is visible:

  int totalHeight = 0;
  foreach (DataGridViewRow row in dataGridView1.Rows)
    totalHeight += row.Height;

  if (totalHeight - dataGridView1.Height < dataGridView1.VerticalScrollingOffset)
  {
    //Last row visible
  }


回答2:

Here's another way to do it...

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