設定DataGridView垂直滾動條

iDotNetSpace發表於2009-03-04

設定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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章