DataGridView控件判断滚动条是否滚动到当前已加载的数据行底部

时间:2023-03-08 16:41:21
private void dgvLoad_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
if (e.NewValue + dgvLoad.DisplayedRowCount(false) == dgvLoad.Rows.Count)
{ MsgShow.Warning(string.Format("NewValue:{0}--OldValue:{1}--DisplayedRowCount:{2}", e.NewValue, e.OldValue,dgvLoad.DisplayedRowCount(false)));
MsgShow.Warning("到底了,可以加载新数据了!");
//这里面写加载数据的相关操作逻辑
}
}
}
        /// <summary>
/// 注册滚动条滚功到末尾时的处理事件
/// </summary>
/// <param name="grid"></param>
/// <param name="onScrollToEnd"></param>
public static void RegistScrollToEndEvent(this DataGridView grid, EventHandler onScrollToEnd)
{
grid.Scroll += new ScrollEventHandler((sender, e) =>
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
if (e.NewValue + grid.DisplayedRowCount(false) == grid.Rows.Count)
{
if (onScrollToEnd != null)
{
onScrollToEnd(grid, null);
}
}
}
});
}
dgvLoad.RegistScrollToEndEvent(dataGrid_OnScrollToEnd);

void dataGrid_OnScrollToEnd(object sender, EventArgs e)
{
MessageBox.Show("load data!");
}