国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Windows Form 分頁 具體實現

2020-01-24 03:00:26
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Collections;

namespace Common
{
    public partial class WinFormPager : UserControl
    {
        public event EventHandler PageChanged; //事件:控件的當前頁碼發(fā)生變更。 
        private int pageSize;
        private int curPage;
        private int pageCount;
        public WinFormPager()
        {
            InitializeComponent(); 
        }

        private void WinFormPager_Load(object sender, EventArgs e)
        {
        }
        /// <summary> 
        /// [屬性]每頁顯示記錄數。 
        /// </summary>
        public int PageSize
        {
            get
            {
                if (pageSize <= 0)
                {
                    pageSize = 10;
                }
                return pageSize;
            }
            set
            {
                pageSize = value;
            }
        }

        /// <summary> 
        /// 當前頁數 
        /// </summary> 
        public int CurPage
        {
            get
            {
                if (curPage <= 0)
                {
                    curPage = 1;
                }
                return curPage;
            }
            set
            {
                curPage = value;
                if (PageChanged != null)
                {
                    SafeRaise.Raise(PageChanged,null);//觸發(fā)當件頁碼變更事件。 
                }
            }
        }
        /// <summary> 
        /// [屬性]總頁數。 
        /// </summary> 
        public int PageCount
        {
            get
            {
                if (RecordCount > 0)
                {
                    int pageCount = RecordCount / PageSize;
                    if (RecordCount % PageSize == 0)
                    {
                      pageCount = RecordCount / PageSize;
                    }

                    else
                    {
                        pageCount = RecordCount / PageSize + 1;
                    }
                    return pageCount;
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                pageCount = value;
            }
        }

        /// <summary> 
        /// [屬性]總記錄數。 
        /// </summary> 
        public int RecordCount
        {
            get;
            set;
        }

        /// <summary> 
        /// [屬性]相對于當前頁的上一頁 
        /// </summary> 
        public int PrevPage
        {
            get
            {
                if (CurPage > 1)
                {
                    return CurPage - 1;
                }
                return 1;
            }
        }

        /// <summary> 
        /// [屬性]相對于當前頁的下一頁 
        /// </summary> 
        public int NextPage
        {
            get
            {
                if (CurPage < PageCount)
                {
                    return CurPage + 1;
                }
                return PageCount;
            }
        }

        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            this.CurPage = 1;
        }

        private void btnLastPage_Click(object sender, EventArgs e)
        {
            this.CurPage = this.PrevPage;
        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            this.CurPage = this.NextPage;
        }

        private void btnEndPage_Click(object sender, EventArgs e)
        {
            this.CurPage = this.PageCount;
        }

        private void txtPageNumber_TextChanged(object sender, EventArgs e)
        {
            if (!Validator.IsNumeric(this.txtPageNumber.Text.Trim()))
            {
                MessageBox.Show("請輸入數字!");
            }
        }

        private void btnJump_Click(object sender, EventArgs e)
        {

            if (!Validator.IsNumeric(this.txtPageNumber.Text.Trim()))//驗證輸入是否為數字
            {
                MessageBox.Show("請輸入數字!");
            }
            else
            {
                if (int.Parse(this.txtPageNumber.Text.Trim()) > 0)
                {
                    if (int.Parse(this.txtPageNumber.Text.Trim()) < this.PageCount)
                    {
                        this.CurPage = int.Parse(this.txtPageNumber.Text.Trim());
                    }
                    else
                    {
                        this.CurPage = this.PageCount;
                    }
                }
                else
                {
                    this.CurPage = 1;
                }
            }
        } 
    }
}


該用戶自定義控件在頁面中取名pager

復制代碼 代碼如下:

private void BindData()
{
    int rowCount = 0;
    pager.PageSize = 15;    DataGridView.DataSource = GetList(pager.CurPage, pager.PageSize, out rowCount);
    pager.RecordCount = rowCount;
    pager.lbNumber.Text = string.Format("共{0}條記錄,每頁{1}條記錄,共{2}頁", pager.RecordCount.ToString(), pager.PageSize.ToString(), pager.PageCount.ToString());
}

private void Pager_PageChanged(object sender, EventArgs e)
{
    BindData(); //重新對DataGridView控件的數據源進行綁定。
}

控件

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤凰县| 望谟县| 威信县| 巴彦淖尔市| 治县。| 尚义县| 招远市| 革吉县| 桦甸市| 荆门市| 松潘县| 衡南县| 盘山县| 治多县| 武威市| 若尔盖县| 图们市| 台安县| 龙陵县| 呼伦贝尔市| 报价| 廉江市| 钟山县| 陵川县| 松溪县| 石楼县| 沛县| 资溪县| 墨脱县| 卢氏县| 齐河县| 闽清县| 安达市| 津市市| 卓尼县| 峡江县| 湛江市| 福安市| 芦溪县| 卢湾区| 肃宁县|