zoukankan      html  css  js  c++  java
  • 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事件里。

  • 相关阅读:
    Windows Server 2008取消登录前的Ctrl+Alt+Delete组合键操作
    Kali Linux远程连接Windows服务器
    Kali Linux虚拟机安装完整安装过程及简单配置(视频)
    Kali Linux中下载工具Axel的安装和使用
    2017年Kali Linux更新源
    解决VMware虚拟机报错“无法连接MKS:套接字连接尝试次数太多,正在放弃”
    .deb软件包的安装和软件的卸载
    解决C语言程序报错:return type defaults to‘int’
    解决BackBox中Fcitx输入法中文输入状态下不显示候选词框的问题
    导航狗信息导航网站首页源代码(2017年11月03日版)
  • 原文地址:https://www.cnblogs.com/njcxwz/p/4607573.html
Copyright © 2011-2022 走看看