1. 程式人生 > >【C#】DataGridView的分頁功能

【C#】DataGridView的分頁功能

1、分頁整體顯示如下:

主要包括功能如下:

1)顯示總頁數、當前頁數

2)向前,向後翻頁功能

3)顯示首頁,顯示最後一頁功能

4)自定義跳轉頁面功能

 

2、頁面設計部分(.Designer.cs檔案):

partial class DGVPager
    {
        /// <summary> 
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
/// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); }
base.Dispose(disposing); } #region 元件設計器生成的程式碼 /// <summary> /// 設計器支援所需的方法 - 不要 /// 使用程式碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.label5 = new System.Windows.Forms.Label(); this.label3 = new
System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.linkLast = new System.Windows.Forms.Button(); this.linkFirst = new System.Windows.Forms.Button(); this.linkPrevious = new System.Windows.Forms.Button(); this.linkNext = new System.Windows.Forms.Button(); this.lblPageCount = new System.Windows.Forms.Label(); this.lblSept = new System.Windows.Forms.Label(); this.lblTotalCount = new System.Windows.Forms.Label(); this.lblCurrentPage = new System.Windows.Forms.Label(); this.btnGo = new System.Windows.Forms.Button(); this.txtPageNum = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // label5 // this.label5.AutoSize = true; this.label5.Font = new System.Drawing.Font("幼圓", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label5.Location = new System.Drawing.Point(196, 6); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(49, 14); this.label5.TabIndex = 264; this.label5.Text = "條記錄"; // // label3 // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("幼圓", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label3.Location = new System.Drawing.Point(1, 6); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(63, 14); this.label3.TabIndex = 263; this.label3.Text = "當前頁:"; // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("幼圓", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.Location = new System.Drawing.Point(145, 6); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(21, 14); this.label1.TabIndex = 261; this.label1.Text = ""; // // linkLast // this.linkLast.Font = new System.Drawing.Font("幼圓", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.linkLast.Location = new System.Drawing.Point(410, 1); this.linkLast.Name = "linkLast"; this.linkLast.Size = new System.Drawing.Size(44, 23); this.linkLast.TabIndex = 260; this.linkLast.Text = ">>|"; this.linkLast.UseVisualStyleBackColor = true; this.linkLast.Click += new System.EventHandler(this.linkLast_Click); // // linkFirst // this.linkFirst.Font = new System.Drawing.Font("幼圓", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.linkFirst.Location = new System.Drawing.Point(255, 1); this.linkFirst.Name = "linkFirst"; this.linkFirst.Size = new System.Drawing.Size(44, 23); this.linkFirst.TabIndex = 259; this.linkFirst.Text = "|<<"; this.linkFirst.UseVisualStyleBackColor = true; this.linkFirst.Click += new System.EventHandler(this.linkFirst_Click); // // linkPrevious // this.linkPrevious.Font = new System.Drawing.Font("幼圓", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.linkPrevious.Location = new System.Drawing.Point(312, 1); this.linkPrevious.Name = "linkPrevious"; this.linkPrevious.Size = new System.Drawing.Size(34, 23); this.linkPrevious.TabIndex = 258; this.linkPrevious.Text = "<"; this.linkPrevious.UseVisualStyleBackColor = true; this.linkPrevious.ClientSizeChanged += new System.EventHandler(this.linkPrevious_Click); this.linkPrevious.Click += new System.EventHandler(this.linkPrevious_Click); // // linkNext // this.linkNext.Font = new System.Drawing.Font("幼圓", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.linkNext.Location = new System.Drawing.Point(361, 1); this.linkNext.Name = "linkNext"; this.linkNext.Size = new System.Drawing.Size(34, 23); this.linkNext.TabIndex = 257; this.linkNext.Text = ">"; this.linkNext.UseVisualStyleBackColor = true; this.linkNext.Click += new System.EventHandler(this.linkNext_Click); // // lblPageCount // this.lblPageCount.AutoSize = true; this.lblPageCount.ForeColor = System.Drawing.Color.Red; this.lblPageCount.Location = new System.Drawing.Point(108, 7); this.lblPageCount.Name = "lblPageCount"; this.lblPageCount.Size = new System.Drawing.Size(11, 12); this.lblPageCount.TabIndex = 256; this.lblPageCount.Text = "1"; // // lblSept // this.lblSept.AutoSize = true; this.lblSept.Location = new System.Drawing.Point(91, 8); this.lblSept.Name = "lblSept"; this.lblSept.Size = new System.Drawing.Size(11, 12); this.lblSept.TabIndex = 255; this.lblSept.Text = "/"; // // lblTotalCount // this.lblTotalCount.AutoSize = true; this.lblTotalCount.ForeColor = System.Drawing.Color.Red; this.lblTotalCount.Location = new System.Drawing.Point(168, 7); this.lblTotalCount.Name = "lblTotalCount"; this.lblTotalCount.Size = new System.Drawing.Size(23, 12); this.lblTotalCount.TabIndex = 254; this.lblTotalCount.Text = "100"; // // lblCurrentPage // this.lblCurrentPage.AutoSize = true; this.lblCurrentPage.ForeColor = System.Drawing.Color.Red; this.lblCurrentPage.Location = new System.Drawing.Point(74, 7); this.lblCurrentPage.Name = "lblCurrentPage"; this.lblCurrentPage.Size = new System.Drawing.Size(11, 12); this.lblCurrentPage.TabIndex = 253; this.lblCurrentPage.Text = "1"; // // btnGo // this.btnGo.BackColor = System.Drawing.Color.Transparent; this.btnGo.ForeColor = System.Drawing.Color.Black; this.btnGo.Location = new System.Drawing.Point(500, 1); this.btnGo.Name = "btnGo"; this.btnGo.Size = new System.Drawing.Size(45, 23); this.btnGo.TabIndex = 252; this.btnGo.Text = "跳轉"; this.btnGo.UseVisualStyleBackColor = false; this.btnGo.Click += new System.EventHandler(this.btnGo_Click); // // txtPageNum // this.txtPageNum.Location = new System.Drawing.Point(465, 2); this.txtPageNum.Name = "txtPageNum"; this.txtPageNum.Size = new System.Drawing.Size(29, 21); this.txtPageNum.TabIndex = 251; this.txtPageNum.TextChanged += new System.EventHandler(this.txtPageNum_TextChanged); this.txtPageNum.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtPageNum_KeyPress); // // DGVPager // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.label5); this.Controls.Add(this.label3); this.Controls.Add(this.label1); this.Controls.Add(this.linkLast); this.Controls.Add(this.linkFirst); this.Controls.Add(this.linkPrevious); this.Controls.Add(this.linkNext); this.Controls.Add(this.lblPageCount); this.Controls.Add(this.lblSept); this.Controls.Add(this.lblTotalCount); this.Controls.Add(this.lblCurrentPage); this.Controls.Add(this.btnGo); this.Controls.Add(this.txtPageNum); this.Name = "DGVPager"; this.Size = new System.Drawing.Size(547, 25); this.Load += new System.EventHandler(this.DGVPager_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button linkLast; private System.Windows.Forms.Button linkFirst; private System.Windows.Forms.Button linkPrevious; private System.Windows.Forms.Button linkNext; private System.Windows.Forms.Label lblPageCount; private System.Windows.Forms.Label lblSept; private System.Windows.Forms.Label lblTotalCount; private System.Windows.Forms.Label lblCurrentPage; private System.Windows.Forms.Button btnGo; private System.Windows.Forms.TextBox txtPageNum; }

3、程式碼功能部分:

public partial class DGVPager : UserControl
    {
        public DGVPager()
        {
            InitializeComponent();
        }

        #region 分頁欄位和屬性

        private int pageIndex = 1;
        /// <summary>
        /// 當前頁數
        /// </summary>
        public virtual int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        private int pageSize = 20;
        /// <summary>
        /// 每頁記錄數
        /// </summary>
        public virtual int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int recordCount = 0;
        /// <summary>
        /// 總記錄數
        /// </summary>
        public virtual int RecordCount
        {
            get { return recordCount; }
            set { recordCount = value; }
        }

        private int pageCount = 0;
        /// <summary>
        /// 總頁數
        /// </summary>
        public int PageCount
        {
            get
            {
                if (pageSize != 0)
                {
                    pageCount = GetPageCount();
                }
                return pageCount;
            }
        }

        #endregion

        #region 頁碼變化時觸發事件

        public event EventHandler OnPageChanged;

        #endregion

        #region 分頁及相關事件功能實現

        /// <summary>
        /// 設窗體控制元件全部可用
        /// </summary>
        private void SetFormCtrEnabled()
        {
            linkFirst.Enabled = true;
            linkPrevious.Enabled = true;
            linkNext.Enabled = true;
            linkLast.Enabled = true;
            btnGo.Enabled = true;
        }

        /// <summary>
        /// 計算總頁數
        /// </summary>
        /// <returns></returns>
        private int GetPageCount()
        {
            if (PageSize == 0)
            {
                return 0;
            }
            int pageCount = RecordCount / PageSize;
            if (RecordCount % PageSize == 0)
            {
                pageCount = RecordCount / PageSize;
            }
            else
            {
                pageCount = RecordCount / PageSize + 1;
            }
            return pageCount;
        }
        /// <summary>
        /// 用於客戶端呼叫
        /// </summary>
        public void DrawControl(int count)
        {
            recordCount = count;
            DrawControl(false);
        }
        /// <summary>
        /// 根據不同的條件,改變頁面控制元件的呈現狀態
        /// </summary>
        private void DrawControl(bool callEvent)
        {

            lblCurrentPage.Text = PageIndex.ToString();
            lblPageCount.Text = PageCount.ToString();
            lblTotalCount.Text = RecordCount.ToString();

            if (callEvent && OnPageChanged != null)
            {
                OnPageChanged(this, null);//當前分頁數字改變時,觸發委託事件
            }
            SetFormCtrEnabled();
            if (PageCount == 1 || PageCount == 0)//有且僅有一頁時
            {
                linkFirst.Enabled = false;
                linkPrevious.Enabled = false;
                linkNext.Enabled = false;
                linkLast.Enabled = false;
                btnGo.Enabled = false;
            }
            else if (PageIndex == 1)//當前頁為第一頁時
            {
                linkFirst.Enabled = false;
                linkPrevious.Enabled = false;
            }
            else if (PageIndex == PageCount)//當前頁為最後一頁時
            {
                linkNext.Enabled = false;
                linkLast.Enabled = false;
            }
        }

        #endregion

        #region 相關控制元件事件

        //首頁按鈕
        private void linkFirst_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            DrawControl(true);
        }

        //上一頁按鈕
        private void linkPrevious_Click(object sender, EventArgs e)
        {
            PageIndex = Math.Max(1, PageIndex - 1);
            DrawControl(true);
        }

        //下一頁按鈕
        private void linkNext_Click(object sender, EventArgs e)
        {
            PageIndex = Math.Min(PageCount, PageIndex + 1);
            DrawControl(true);
        }

        //尾頁按鈕
        private void linkLast_Click(object sender, EventArgs e)
        {
            PageIndex = PageCount;
            DrawControl(true);
        }

        /// <summary>
        /// 按下enter鍵,執行跳轉頁面功能
        /// </summary>
        private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            btnGo_Click(null, null);
        }

        /// <summary>
        /// 跳轉頁數限制
        /// </summary>
        private void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num = 0;
            if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
            {   //TryParse 函式,將字串轉換成等效的整數,返回bool型,判斷是否轉換成功。
                //輸入除數字以外的字元是轉換不成功的

                if (num > PageCount)   //輸入數量大於最大頁數時,文字框自動顯示最大頁數
                {
                    txtPageNum.Text = PageCount.ToString();
                }
            }
        }

        /// <summary>
        /// 跳轉按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGo_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
            {
                PageIndex = num;
                DrawControl(true);
            }
        }

        #endregion

        bool isTextChanged = false;

        /// <summary>
        /// 游標離開 每頁設定文字框時,顯示到首頁
        private void txtPageSize_Leave(object sender, EventArgs e)
        {
            if (isTextChanged)
            {
                isTextChanged = false;
                linkFirst_Click(null, null);
            }
        }

        private void DGVPager_Load(object sender, EventArgs e)
        {

        }
    }

4、DGVPager類的使用:

int pagePTR = 0;//設定Page標誌位