自定义分页控件二

using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace KK.Pagers
{
    public partial class Pagers : UserControl
    {
        #region 定义、构造
        private int _pageIndex = 1;
        /// <summary>
        /// 当前页数
        /// </summary> 
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        private int _pageSize;
        /// <summary>
        /// 每一页显示的数据量
        /// </summary> 
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        private int _pageCount;
        /// <summary>
        /// 总数据数
        /// </summary>
        public int PageCount
        {
            get
            {
                if (PageSize != 0) _pageCount = GetPageCount();
                return _pageCount;
            }
        }

        private int _totalCount = 20;
        /// <summary>
        /// 总数
        /// </summary>
        public int TotalCount
        {
            get { return _totalCount; }
            set { _totalCount = value; }
        }

        /// <summary>
        /// 首页背景图
        /// </summary>
        public Image FirstBackgroundImage
        {
            get { return pboxFirst.Image; }
            set { pboxFirst.Image = value; }
        }

        /// <summary>
        /// 上一页背景图
        /// </summary>
        public Image LastBackgroundImage
        {
            get { return this.pboxUp.Image; }
            set { pboxUp.Image = value; }
        }

        /// <summary>
        /// 下一个背景图
        /// </summary>
        public Image NextBackgroundImage
        {
            get { return this.pboxNext.Image; }
            set { pboxNext.Image = value; }
        }

        /// <summary>
        /// 尾页背景图
        /// </summary>
        public Image EndBackgroundImage
        {
            get { return this.pboxEnd.Image; }
            set
            {
                pboxEnd.Image = value;
            }
        }

        /// <summary>
        /// 构造
        /// </summary>
        public Pagers()
        {
            InitializeComponent();
            this.Load += Pagers_Load;
        }

        #endregion

        #region event

        void Pagers_Load(object sender, EventArgs e)
        {
            PageSize = int.Parse(txtPageSize.Text);
            if (PageIndex <= 0)
                PageIndex = 1;
        }

        [Browsable(true), Description("分页改变事件"), Category("操作")]
        public event EventHandler OnPageChanged;
        //首页
        private void pboxPageFirst_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            DrawControl(true);
        }

        //上一页
        private void pboxPre_Click(object sender, EventArgs e)
        {
            if (PageIndex > 1) PageIndex--;

            DrawControl(true);
        }

        //下一页
        private void pboxNext_Click(object sender, EventArgs e)
        {
            if (PageIndex < PageCount) PageIndex++;

            DrawControl(true);
        }

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

        private void ctboxCurrentPageIndex_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int page = -1;
                if (int.TryParse(txtPageIndex.Text, out page)
                    && int.Parse(txtPageIndex.Text) > 0
                    && int.Parse(txtPageIndex.Text) <= PageCount
                    && PageIndex != int.Parse(txtPageIndex.Text))
                {
                    PageIndex = int.Parse(txtPageIndex.Text);
                    DrawControl(true);
                }
                else
                {
                    txtPageIndex.Text = PageIndex.ToString();
                    txtPageIndex.SelectionStart = txtPageIndex.TextLength;
                }
            }
        }

        private void ctboxCurrentPageIndex_TextChanged(object sender, EventArgs e)
        {
            Regex reg = new Regex("^[0-9]*$");
            if (!reg.IsMatch(txtPageIndex.Text))
            {
                txtPageIndex.Text = PageIndex.ToString();
                txtPageIndex.SelectionStart = txtPageIndex.TextLength;
            }
            if (PageCount.Equals(0))
            {
                DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1);
                DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1);
                DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1);
                DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1);
                return;
            }
            //首页且不止一页数据
            if (PageIndex.Equals(1) && !PageIndex.Equals(PageCount))
            {
                DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1);
                DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1);
                DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next);
                DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End);
                return;
            }
            //只有一页数据
            if (PageIndex.Equals(1) && PageIndex.Equals(PageCount))
            {
                DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1);
                DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1);
                DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1);
                DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1);
                return;
            }
            //尾页
            if (PageIndex == PageCount)
            {
                DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First);
                DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last);
                DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1);
                DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1);
                return;
            }
            //大于第一页
            if (PageIndex > 1 && PageIndex < PageCount)
            {
                DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First);
                DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last);
                DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next);
                DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End);
                return;
            }
        }

        #endregion

        #region private method
        /// <summary>
        /// 设置控件是否显示
        /// </summary>
        /// <param name="pb">PictureBox pb</param>
        /// <param name="img">图片</param>
        private void DisplayImage(PictureBox pb, Image img)
        {
            if (img != null) pb.Image = img;
        }

        /// <summary>
        /// 获取页数
        /// </summary>
        /// <returns>页数</returns>
        private int GetPageCount()
        {
            if (PageSize == 0) return 0;

            int pageCount = TotalCount / PageSize;
            if (TotalCount % PageSize == 0) pageCount = TotalCount / PageSize;
            else pageCount = TotalCount / PageSize + 1;

            return pageCount;
        }

        /// <summary>
        /// 显示页数
        /// </summary>
        /// <param name="count">页数</param>
        public void DrawControl(int count)
        {
            TotalCount = count;
            this.lblPageCountShow.Text = string.Format("共{0}页", PageCount);
            DrawControl(false);
        }

        /// <summary>
        /// 分页控件可用性设置
        /// </summary>
        /// <param name="callEvent">callEvent</param>
        private void DrawControl(bool callEvent)
        {
            this.txtPageIndex.Text = PageIndex.ToString();
            this.txtPageIndex.SelectionStart = txtPageIndex.TextLength;
            if (callEvent && OnPageChanged != null)
                OnPageChanged(this, null);
            SetFormCtrEnabled();
            if (PageCount == 0)
            {
                this.txtPageIndex.Text = "1";
                this.txtPageIndex.SelectionStart = txtPageIndex.TextLength;
                this.pboxFirst.Enabled = false;
                this.pboxUp.Enabled = false;
                this.pboxNext.Enabled = false;
                this.pboxEnd.Enabled = false;
            }
            if (PageCount == 1)
            {
                this.txtPageIndex.Text = "1";
                this.txtPageIndex.SelectionStart = txtPageIndex.TextLength;
                this.pboxFirst.Enabled = false;
                this.pboxUp.Enabled = false;
                this.pboxNext.Enabled = false;
                this.pboxEnd.Enabled = false;
            }
            else if (PageIndex == 1)
            {
                this.txtPageIndex.Text = "1";
                this.txtPageIndex.SelectionStart = txtPageIndex.TextLength;
                this.pboxFirst.Enabled = false;
                this.pboxUp.Enabled = false;
            }
            else if (PageIndex == PageCount)
            {
                this.txtPageIndex.Text = PageCount.ToString();
                this.txtPageIndex.SelectionStart = txtPageIndex.TextLength;
                this.pboxNext.Enabled = false;
                this.pboxEnd.Enabled = false;
            }
        }

        private void SetFormCtrEnabled()
        {
            this.pboxFirst.Enabled = true;
            this.pboxNext.Enabled = true;
            this.pboxUp.Enabled = true;
            this.pboxEnd.Enabled = true;
        }

        #endregion

        private void Pbox_EnabledChanged(object sender, EventArgs e)
        {
            PictureBox pbox = sender as PictureBox;
            bool isEnable = pbox.Enabled;
            Image pageimage;
            switch (pbox.Name)
            {
                case "pboxFirst":
                    pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_First : global::KK.Pagers.Properties.Resources.Page_First1;
                    break;
                case "pboxNext":
                    pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_Next : global::KK.Pagers.Properties.Resources.Page_Next1;
                    break;
                case "pboxUp":
                    pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_Last : global::KK.Pagers.Properties.Resources.Page_Last1;
                    break;
                default:
                    pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_End : global::KK.Pagers.Properties.Resources.Page_End1;
                    break;
            }
            DisplayImage(pbox, pageimage);
        }

        private void Pagers_Load_1(object sender, EventArgs e)
        {
            if (GetPageCount() > 0) txtPageIndex.Text = "1";
            else txtPageIndex.Text = "0";
        }

        private void txtPageSize_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                Regex reg = new Regex("^[0-9]*$");
                if (reg.IsMatch(txtPageSize.Text))
                {
                    this.PageSize = int.Parse(txtPageSize.Text);
                    PageIndex = int.Parse(this.txtPageIndex.Text);
                    DrawControl(true);
                }
            }
        }
    }
}

 应用方式:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Demo
{
    public partial class Form2 : Form
    {
        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                SolidBrush b = new SolidBrush(this.dgvList.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture),
                    this.dgvList.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
            }
            catch { return; }
        }

        List<StudentInfo> list = new List<StudentInfo>();

        public Form2()
        {
            InitializeComponent();
            this.dgvList.AutoGenerateColumns = false;

            this.pagingList.PageIndex = 1;
            for (int i = 1; i <= 200; i++)
            {
                StudentInfo stu = new StudentInfo();
                stu.NameA = "学生aaaaaaaa" + i;
                stu.Age = i;
                list.Add(stu);
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            AddCheckeBoxToDGVHeader(this.dgvList);

            GetPageData();
        }

        private void AddCheckeBoxToDGVHeader(DataGridView dgv)
        {
            for (int i = 0; i < this.dgvList.Columns.Count; i++)
            {
                System.Windows.Forms.CheckBox ckBox = new System.Windows.Forms.CheckBox();
                ckBox.Text = "全选";
                ckBox.Checked = true;
                System.Drawing.Rectangle rect =
                    dgv.GetCellDisplayRectangle(i, -1, false);
                ckBox.Size = new System.Drawing.Size(50, 25);

                ckBox.Location = rect.Location;
                ckBox.Padding = new System.Windows.Forms.Padding(2, 6, 0, 0);
                ckBox.BackColor = Color.Transparent;
                ckBox.Name = dgv.Columns[i].Name;
                ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);

                dgv.Controls.Add(ckBox);
            }
        }

        void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chb = sender as CheckBox;
            MessageBox.Show("Test=="+ chb.Name);
        }  

        private void GetPageData()
        {
            List<StudentInfo> dataSource = list
                .Skip((this.pagingList.PageIndex - 1) * this.pagingList.PageSize)
                .Take(this.pagingList.PageSize).ToList();
            var count = Math.Ceiling(double.Parse(Convert.ToString(list.Count / dataSource.Count)));

          
            this.pagingList.TotalCount = list.Count;
            //调用page的公用方法
            this.pagingList.DrawControl(list.Count);
            this.dgvList.DataSource = dataSource;
        }

        private void pagingList_OnPageChanged(object sender, EventArgs e)
        {
            GetPageData();
        }
    }

    public class StudentInfo
    {
        public string NameA { get; set; }
        public int Age { get; set; }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/YYkun/p/9254021.html