Winform自定义分页控件的实现

实现效果 有点丑陋 但是功能是没问题的 测试过

 

实现思路

先创建一个用户控件

代码实现

 public partial class PagerControl : UserControl
    {
        private int record = 0;

        /// <summary>
        /// 总记录数
        /// </summary>
        public int Record
        {
            get { return record; }
            set
            {
                record = value; 
                InitPageInfo();
            }
        }

        private int pageSize = 20;

        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int currentPage = 1;

        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }

        public int pageNum = 0;

        /// <summary>
        /// 总页码
        /// </summary>
        public int PageNum
        {
            get
            {
                if (Record == 0)
                {
                    pageNum = 0;
                }
                else
                {
                    if (Record % PageSize > 0)
                    {
                        pageNum = Record / PageSize + 1;
                    }
                    else
                    {
                        pageNum = Record / PageSize;
                    }
                }
                return pageNum;
            }

        }

        //定义委托
        public delegate void BindHandle(object sender, EventArgs e);

        /// <summary>
        /// 绑定数据源事件
        /// </summary>
        public event BindHandle BindSource;

        public PagerControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == 1)
                {
                    MessageBox.Show("当前已经是首页");
                    return;
                }
                else
                {
                    CurrentPage = 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
           
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == 1)
                {
                    MessageBox.Show("当前已经是首页");
                    return;
                }
                else
                {
                    CurrentPage = CurrentPage - 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == PageNum)
                {
                    MessageBox.Show("当前已经是末页");
                    return;
                }
                else
                {
                    CurrentPage = CurrentPage + 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == PageNum)
                {
                    MessageBox.Show("当前已经是末页");
                    return;
                }
                else
                {
                    CurrentPage = PageNum;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
        }

         private void InitPageInfo()
        {
             if (Record == 0 || (Record > 0 && CurrentPage > pageNum))
            {
                CurrentPage = 1;
            }
            lblInfo.Text = string.Format("共 {0} 条记录  共 {1} 页  当前第 {2} 页", Record, PageNum, CurrentPage);
            txtPage.Text = CurrentPage.ToString();

        }
private void btnGo_Click(object sender, EventArgs e) { if (Record > 0) { if (!string.IsNullOrEmpty(txtPage.Text) && !Regex.IsMatch(txtPage.Text, @"^[\d]*$")) { MessageBox.Show("请正确填写页码!"); return; } int page = Convert.ToInt32(txtPage.Text); if (page == 0) { page = 1; } if (page > PageNum) { page = PageNum; } CurrentPage = page; if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } } private void PagerControl_Load(object sender, EventArgs e) { if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } }

 

 

使用

只要在窗体中 写好绑定方法

 private void Bind()
        {
            string start = dtpDate1.Value.ToString("yyyy-MM-dd");
            string end = dtpDate2.Value.ToString("yyyy-MM-dd");
            string team = cbxTeam.SelectedValue.ToString();
            string jieshu = cbxSFJS.SelectedValue.ToString();
            int record = 0;
            DataTable dt = eventBiz.GetEvents(start, end, team, jieshu, pagerControl1.CurrentPage, pagerControl1.PageSize,out record);
            pagerControl1.Record = record;
            
            dgvEvent.AutoGenerateColumns = false;
            dgvEvent.DataSource = dt.DefaultView;
        }

 

捆绑绑定事件

 /// <summary>
        /// 绑定数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pagerControl1_BindSource(object sender, EventArgs e)
        {
            Bind();
        }

 

就可以了  需要注意的事情是由于分页控件load事件里会调用bind方法,会用到一些窗体元素的值,所以窗体元素项的初始化,应该放在窗体构造函数中,不要放在窗体load事件里。

 

转载于:https://www.cnblogs.com/njcxwz/p/4607573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值