winform自定义分页控件

1、控件代码:

public partial class PagerControl : UserControl {
        #region 构造函数

        public PagerControl() {
            InitializeComponent();
        }

        #endregion

        #region 分页字段和属性

        private int pageIndex = 1;
        /// 
        /// 当前页面
        /// 
        public virtual int PageIndex {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        private int pageSize = 1;
        /// 
        /// 每页记录数
        /// 
        public virtual int PageSize {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int recordCount = 0;
        /// 
        /// 总记录数
        /// 
        public virtual int RecordCount {
            get { return recordCount; }
            set { recordCount = value; }
        }

        private int pageCount = 0;
        /// 
        /// 总页数
        /// 
        public int PageCount {
            get {
                if (pageSize != 0) {
                    pageCount = GetPageCount();
                }
                return pageCount;
            }
        }
        /// 
        /// 计算总页数
        /// 
        /// 
        private int GetPageCount() {
            if (PageSize == 0) {
                return 0;
            }
            int pageCount = RecordCount / PageSize;
            if (RecordCount % PageSize == 0) {
                pageCount = RecordCount / PageSize;
            }
            else {
                pageCount = RecordCount / PageSize + 1;
            }
            return pageCount;
        }
        #endregion

        public event EventHandler OnPageChanged;

        /// 
        /// 外部调用
        /// 
        public void DrawControl(int count) {
            recordCount = count;
            DrawControl(false);
        }
        /// 
        /// 页面控件呈现
        /// 
        private void DrawControl(bool callEvent) {
            this.GoItem.Text = "跳转";
            this.CurrentItem.Text = PageIndex.ToString();
            this.TolCountItem.Text = RecordCount.ToString();
            this.PageSizeItem.Text = PageSize.ToString();

            if (callEvent && OnPageChanged != null) {
                OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
            }
            SetFormCtrEnabled();
            if (PageCount == 1)//有且仅有一页
            {
                this.MoveFirstItem.Enabled = false;
                this.MovePreviousItem.Enabled = false;
                this.MoveNextItem.Enabled = false;
                this.MoveLastItem.Enabled = false;
            }
            else if (PageIndex == 1)//第一页
            {
                this.MoveFirstItem.Enabled = false;
                this.MovePreviousItem.Enabled = false;
            }
            else if (PageIndex == PageCount)//最后一页
            {
                this.MoveNextItem.Enabled = false;
                this.MoveLastItem.Enabled = false;
            }
        }

        /// 
        /// 设置控件可用性
        /// 
        private void SetFormCtrEnabled() {
            this.CurrentItem.Enabled = true;
            this.MoveFirstItem.Enabled = true;
            this.MovePreviousItem.Enabled = true;
            this.MoveNextItem.Enabled = true;
            this.MoveLastItem.Enabled = true;
            this.GoItem.Enabled = true;
        }

        /// 
        /// 首页
        /// 
        /// 
        /// 
        private void MoveFirstItem_Click(object sender, EventArgs e) {
            PageIndex = 1;
            DrawControl(true);
        }

        /// 
        /// 上一页
        /// 
        /// 
        /// 
        private void MovePreviousItem_Click(object sender, EventArgs e) {
            PageIndex = Math.Max(1, PageIndex - 1);
            DrawControl(true);
        }

        /// 
        /// 下一页
        /// 
        /// 
        /// 
        private void MoveNextItem_Click(object sender, EventArgs e) {
            PageIndex = Math.Min(PageCount, PageIndex + 1);
            DrawControl(true);
        }

        /// 
        /// 末页
        /// 
        /// 
        /// 
        private void MoveLastItem_Click(object sender, EventArgs e) {
            PageIndex = PageCount;
            DrawControl(true);
        }

        /// 
        /// 跳转
        /// 
        /// 
        /// 
        private void GoItem_Click(object sender, EventArgs e) {
            int num = 0;
            if (int.TryParse(this.CurrentItem.Text.Trim(), out num) && num > 0) {
                PageIndex = num;
                DrawControl(true);
            }
        }

        /// 
        /// 跳转页数限制
        /// 
        /// 
        /// 
        private void CurrentItem_TextChanged(object sender, EventArgs e) {
            int num = 0;
            if (int.TryParse(this.CurrentItem.Text.Trim(), out num) && num > 0) {
                if (num > PageCount) {
                    this.CurrentItem.Text = PageCount.ToString();
                }
            }
        }

        /// 
        /// 页数Enter键
        /// 
        /// 
        /// 
        private void CurrentItem_KeyPress(object sender, KeyPressEventArgs e) {
            GoItem_Click(null, null);
        }

        private bool isTextChanged = false;

        /// 
        /// 页数改变事件
        /// 
        /// 
        /// 
        private void PageSizeItem_TextChanged(object sender, EventArgs e) {
            int num = 0;
            if (!int.TryParse(this.PageSizeItem.Text.Trim(), out num) || num <= 0) {
                num = 20;
                this.PageSizeItem.Text = "20";
            }
            else {
                isTextChanged = true;
            }
            pageSize = num;
        }

        /// 
        /// 页数Enter事件
        /// 
        /// 
        /// 
        private void PageSizeItem_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == System.Convert.ToChar(13)) {
                if (isTextChanged) {
                    isTextChanged = false;
                    MoveFirstItem_Click(null, null);
                }
            }
        }
    } 

2、调用代码:

        /// 
        /// 加载数据
        /// 
        /// 
        /// 
        private void SelectForm_Load(object sender, EventArgs e) {
            LoadData();
            pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
        }

        /// 
        /// 分页控件改变事件
        /// 
        /// 
        /// 
        void pagerControl1_OnPageChanged(object sender, EventArgs e) {
            LoadData();
        }

        /// 
        /// 加载数据    
        /// 
        /// 
        /// 
        private void LoadData() {
            List