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事件里。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM