ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 设置DataGridView垂直滚动条

设置DataGridView垂直滚动条

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-03-04 15:48:51 0 删除 编辑

设置DataGridView垂直滚动条

1,

#region 设置DataGridView垂直滚动条的值方法
        ///


        /// 设置DataGridView垂直滚动条的值
        ///

        /// 要显示滚动条的DataGridView对象
        /// 滚动条对象
        /// 是否滚动到底部
        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 垂直滚动条拖动时执行方法
        ///


        /// 垂直滚动条拖动时执行
        ///

        /// 要显示滚动条的DataGridView对象
        /// 滚动条对象
        /// 滚动事件参数
        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.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-561372/,如需转载,请注明出处,否则将追究法律责任。

上一篇: C#事件处理机制
请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5346419