设置DataGridView垂直滚动条
#region 设置DataGridView垂直滚动条的值方法
/// <summary>
/// 设置DataGridView垂直滚动条的值
/// </summary>
/// <param name="dgv">要显示滚动条的DataGridView对象</param>
/// <param name="scroll">滚动条对象</param>
/// <param name="isScrollToBottom">是否滚动到底部</param>
public static void SetScroll(DataGridView dgv, IcmsVScrollBar scroll, bool isScrollToBottom)
{if (dgv == null
|| scroll == null
|| dgv.Rows.Count <= 0)
scroll.Visible = false;
else
{
int s = 0;
if (dgv.ColumnHeadersVisible)
s = dgv.ColumnHeadersHeight + (dgv.Rows.Count * dgv.Rows[0].Height);
else
s = dgv.Rows.Count * dgv.Rows[0].Height;if (s > dgv.Height)
{
scroll.MaxValue = s + 10;
scroll.Visible = true;
scroll.AutoScrollPosition = new Point(0,
(dgv.FirstDisplayedScrollingRowIndex * dgv.Rows[0].Height) + dgv.ColumnHeadersHeight);if (isScrollToBottom)
{
scroll.AutoScrollPosition = new Point(0, scroll.MaxValue);
}
}
else
{
scroll.Visible = false;
}
}}
#endregion
2,
#region 垂直滚动条拖动时执行方法
/// <summary>
/// 垂直滚动条拖动时执行
/// </summary>
/// <param name="dgv">要显示滚动条的DataGridView对象</param>
/// <param name="scroll">滚动条对象</param>
/// <param name="e">滚动事件参数</param>
public static void ToScroll(DataGridView dgv, IcmsVScrollBar scroll, ScrollEventArgs e)
{
if (dgv == null
|| scroll == null
|| dgv.Rows.Count <= 0)
return;int ci = dgv.FirstDisplayedScrollingRowIndex;
if (e.NewValue > e.OldValue)
{
ci = ci + (e.NewValue - e.OldValue) / dgv.Rows[0].Height;
if (ci < dgv.RowCount)
{
dgv.FirstDisplayedScrollingRowIndex = ci;
}
else
{
dgv.FirstDisplayedScrollingRowIndex = dgv.RowCount - 1;
}
}
else if (e.NewValue < e.OldValue)
{
ci = ci - (e.OldValue - e.NewValue) / dgv.Rows[0].Height;
if (ci >= 0)
{
dgv.FirstDisplayedScrollingRowIndex = ci;
}
else
{
dgv.FirstDisplayedScrollingRowIndex = 0;
}
}
scroll.AutoScrollPosition = new Point(0,
(dgv.FirstDisplayedScrollingRowIndex * dgv.Rows[0].Height) + dgv.ColumnHeadersHeight);Debug.WriteLine(scroll.AutoScrollPosition.ToString() + ", " + dgv.FirstDisplayedScrollingRowIndex.ToString());
}
#endregion
3,DataGridView加载万数据后的事件: DataBindingComplete