[DataGridView] Paging implement of the DataGridView

[DataGridView] Paging implement of the DataGridView

image

.cs File


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

namespace Paging_implement_of_the_DataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int dgvTotalPage, dgvCurrentPage, dgvColumnIndex, dbDataLength;
        int dgvRowNumPerPage = 10; //default 一頁10筆
        private string beforeLink; //紀錄前一個點擊的button
        object[][] dbData;

        private void Form1_Load(object sender, EventArgs e)
        {
            dbData = new object[][]
            {new object[]{1}, new object[]{2} , new object[]{3} , new object[]{4} , new object[]{5}
            , new object[]{6}, new object[]{7}, new object[]{8}, new object[]{9}, new object[]{10}
            , new object[]{11} , new object[]{12} , new object[]{13} , new object[]{14} , new object[]{15}
            , new object[]{16} , new object[]{17}, new object[]{18} , new object[]{19} , new object[]{20}
            , new object[]{21}};

            dbDataLength = dbData.Length;

            //計算總頁數
            if (dbDataLength % dgvRowNumPerPage != 0)
                dgvTotalPage = dbDataLength / dgvRowNumPerPage + 1;
            else
                dgvTotalPage = dbDataLength / dgvRowNumPerPage;

            //default不排序dgv
            dgvColumnIndex = -1;

            //default的目前頁數為1
            dgvCurrentPage = 1;

            //設定dgv頁碼
            SetDgvIniPage(dgvTotalPage);

            //將資料塞入dgv
            DataToFillDgv();
        }

        private void SetDgvIniPage(int pageCount)
        {
            //清除所有頁碼相關元件
            pagingPanel.Controls.Clear();

            //宣告頁碼button
            LinkLabel pager;

            //目前畫面頁碼的最後一個數字
            int endNo;

            //頁碼數字初始X座標
            int pointX = 102;

            if (dgvCurrentPage != 1)
            {
                pageCount += dgvCurrentPage - 1;
                if (((pageCount / 10) + 1) > 1)
                {
                    if ((dgvCurrentPage + 9) > pageCount)
                        endNo = pageCount;
                    else
                        endNo = dgvCurrentPage + 9;
                }
                else
                    endNo = pageCount;

                for (int startNo = dgvCurrentPage; startNo <= endNo; startNo++)
                {
                    pager = new LinkLabel();
                    pager.Name = "_" + startNo.ToString();
                    pager.Text = startNo.ToString();
                    pager.AutoSize = true;
                    pager.Size = new System.Drawing.Size(20, 10);
                    pager.Location = new System.Drawing.Point(pointX, 5);
                    pager.ForeColor = System.Drawing.Color.Black;
                    pager.LinkColor = System.Drawing.Color.Black;
                    pager.Click += new EventHandler(Page_LinkLabelClick);
                    pagingPanel.Controls.Add(pager);
                    pointX += pager.Width + 10;
                }
            }
            else
            {
                if (pageCount > 10)
                    endNo = 10;
                else
                    endNo = pageCount;

                for (int startNo = 1; startNo <= endNo; startNo++)
                {
                    pager = new LinkLabel();
                    pager.Name = "_" + startNo.ToString();
                    pager.Text = startNo.ToString();
                    pager.AutoSize = true;
                    pager.Size = new System.Drawing.Size(20, 10);
                    pager.Location = new System.Drawing.Point(pointX, 5);
                    pager.ForeColor = System.Drawing.Color.Black;
                    pager.LinkColor = System.Drawing.Color.Black;
                    pager.Click += new EventHandler(Page_LinkLabelClick);
                    pagingPanel.Controls.Add(pager);
                    pointX += pager.Width + 10;
                }
            }

            LinkLabel firstLinkLabel = new LinkLabel();
            firstLinkLabel.Name = "firstLinkLabel";
            firstLinkLabel.Text = "第一頁";
            firstLinkLabel.Size = new System.Drawing.Size(45, 20);
            firstLinkLabel.Location = new System.Drawing.Point(0, 5);
            firstLinkLabel.ForeColor = System.Drawing.Color.Black;
            firstLinkLabel.LinkColor = System.Drawing.Color.Black;
            firstLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            pagingPanel.Controls.Add(firstLinkLabel);
 
            LinkLabel previousTenLinkLabel = new LinkLabel();
            previousTenLinkLabel.Name = "previousTenLinkLabel";
            previousTenLinkLabel.Text = "前十頁";
            previousTenLinkLabel.Size = new System.Drawing.Size(50, 20);
            previousTenLinkLabel.Location = new System.Drawing.Point(50, 5);
            previousTenLinkLabel.ForeColor = System.Drawing.Color.Black;
            previousTenLinkLabel.LinkColor = System.Drawing.Color.Black;
            previousTenLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            pagingPanel.Controls.Add(previousTenLinkLabel);
 
            LinkLabel afterTenLinkLabel = new LinkLabel();
            afterTenLinkLabel.Name = "afterTenLinkLabel";
            afterTenLinkLabel.Text = "下十頁";
            afterTenLinkLabel.Size = new System.Drawing.Size(50, 20);
            afterTenLinkLabel.Location = new System.Drawing.Point(pointX + 10, 5);
            afterTenLinkLabel.ForeColor = System.Drawing.Color.Black;
            afterTenLinkLabel.LinkColor = System.Drawing.Color.Black;
            afterTenLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            pagingPanel.Controls.Add(afterTenLinkLabel);
 
            pointX += afterTenLinkLabel.Width + 10;

            LinkLabel lastLinkLabel = new LinkLabel();
            lastLinkLabel.Name = "lastLinkLabel";
            lastLinkLabel.Text = "最後一頁";
            lastLinkLabel.Size = new System.Drawing.Size(80, 20);
            lastLinkLabel.Location = new System.Drawing.Point(pointX, 5);
            lastLinkLabel.ForeColor = System.Drawing.Color.Black;
            lastLinkLabel.LinkColor = System.Drawing.Color.Black;
            lastLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            pagingPanel.Controls.Add(lastLinkLabel);

            if (dgvCurrentPage != 1)
                dgvTotalPage = pageCount;

            if (dgvTotalPage <= 10)
            {
                previousTenLinkLabel.Enabled = false;
                afterTenLinkLabel.Enabled = false;
            }

            if (dgvTotalPage == 1)
                lastLinkLabel.Enabled = false;

            Label promptLabel_1 = new Label();
            promptLabel_1.Text = "每頁顯示資料筆數:";
            promptLabel_1.AutoSize = true;
            promptLabel_1.Location = new System.Drawing.Point(600, 5);
            promptLabel_1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((int)(((byte)(62)))));
            pagingPanel.Controls.Add(promptLabel_1);
 
            TextBox promptTextBox = new TextBox();
            promptTextBox.Name = "promptTextBox";
            promptTextBox.Location = new System.Drawing.Point(717, 2);
            promptTextBox.Size = new System.Drawing.Size(40, 15);
            promptTextBox.Text = dgvRowNumPerPage.ToString();
            promptTextBox.ImeMode = ImeMode.Disable;
            promptTextBox.KeyDown += new KeyEventHandler(TextKeyDown_num);
            promptTextBox.KeyDown += new KeyEventHandler(promptTextBox_KeyDown);
            pagingPanel.Controls.Add(promptTextBox);

            Label promptLabel_2 = new Label();
            promptLabel_2.Text = "上限50筆";
            promptLabel_2.AutoSize = true;
            promptLabel_2.Location = new System.Drawing.Point(770, 5);
            promptLabel_2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((int)(((byte)(62)))));
            pagingPanel.Controls.Add(promptLabel_2);
        }

        private void promptTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
                return;

            if (string.IsNullOrEmpty(((TextBox)sender).Text))
                return;
 
            if ( Convert.ToInt32(((TextBox)sender).Text) == 0 )
            {
                ((TextBox)sender).Text = dgvRowNumPerPage.ToString();

                //"請輸入大於0的數字"
                MessageBox.Show("請輸入大於0的數字", "i-TEM", MessageBoxButtons.OK);

                return;
            }

            int test = 0;

            if (!int.TryParse(((TextBox)sender).Text, out test))
            {
                ((TextBox)sender).Text = dgvRowNumPerPage.ToString();

                //"請輸入正整數"
                MessageBox.Show("請輸入正整數", "i-TEM", MessageBoxButtons.OK);

                return;
            }
 
            dgvRowNumPerPage = Convert.ToInt32(((TextBox)sender).Text);

            if (dgvRowNumPerPage > 50)
            {
                MessageBox.Show("超過上限50筆", "i-TEM", MessageBoxButtons.OK);

                return;
            }

            if (dbDataLength % dgvRowNumPerPage != 0)
                dgvTotalPage = dbDataLength / dgvRowNumPerPage + 1;
            else
                dgvTotalPage = dbDataLength / dgvRowNumPerPage;

            dgvCurrentPage = 1;
            SetDgvIniPage(dgvTotalPage);
            DataToFillDgv();
        }

        private void TextKeyDown_num(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
                (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) ||
                e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete ||
                e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                e.SuppressKeyPress = false;
            else
                e.SuppressKeyPress = true;
        }
 
        /// <summary>
        /// 資料排序
        /// </summary>
        /// <param>資料來源</param>
        /// <param>排序第幾個欄位</param>
        /// <returns>排序完資料</returns>
        private object[][] Sorting(object[][] sourceData, int columnNo)
        {
            if (sourceData.Length < 2)
                return sourceData;
            int dataRow = 0;
            for (int i = 0; i < sourceData.Length; i++)
            {
                if (sourceData[i][columnNo] != null)
                {
                    dataRow = i;
                    break;
                }
            }

            if (sourceData[dataRow][columnNo] == null)
                return sourceData;

            Type compareType = sourceData[dataRow][columnNo].GetType();
            string typeName = compareType.Name;
            for (int j = 0; j < sourceData.Length; j++)
            {
                for (int i = 0; i < sourceData.Length - 1; i++)
                {
                    switch (typeName)
                    {
                        case "Int32":
                        case "Decimal":
                            int currentValue = 0;
                            int nextValue = 0;

                            if (sourceData[i][columnNo] != null)
                                currentValue = Convert.ToInt32(sourceData[i][columnNo]);

                            if (sourceData[i + 1][columnNo] != null)
                                nextValue = Convert.ToInt32(sourceData[i + 1][columnNo]);

                            if (currentValue > nextValue)
                            {
                                object[] hold;
                                hold = sourceData[i];
                                sourceData[i] = sourceData[i + 1];
                                sourceData[i + 1] = hold;
                            }

                            break;

                        case "Double":
                            double currentDouble = 0;
                            double nextDouble = 0;

                            if (sourceData[i][columnNo] != null)
                                currentDouble = Convert.ToDouble(sourceData[i][columnNo]);

                            if (sourceData[i + 1][columnNo] != null)
                                nextDouble = Convert.ToDouble(sourceData[i + 1][columnNo]);

                             if (currentDouble > nextDouble)
                            {
                                object[] hold;
                                hold = sourceData[i];
                                sourceData[i] = sourceData[i + 1];
                                sourceData[i + 1] = hold;
                            }

                            break;

                        case "String":
                            string currentStr = string.Empty;
                            string nextStr = string.Empty;

                            if (sourceData[i][columnNo] != null)
                                currentStr = Convert.ToString(sourceData[i][columnNo]);

                            if (sourceData[i + 1][columnNo] != null)
                                nextStr = Convert.ToString(sourceData[i + 1][columnNo]);

                            if (string.Compare(currentStr, nextStr) > 0)
                            {
                                object[] hold;
                                hold = sourceData[i];
                                sourceData[i] = sourceData[i + 1];
                                sourceData[i + 1] = hold;
                            }

                            break;

                        case "DateTime":
                            DateTime currentDatetime = DateTime.MinValue;
                            DateTime nextDatetime = DateTime.MinValue;

                            if (sourceData[i][columnNo] != null)
                                currentDatetime = Convert.ToDateTime(sourceData[i][columnNo]);

                            if (sourceData[i + 1][columnNo] != null)
                                nextDatetime = Convert.ToDateTime(sourceData[i + 1][columnNo]);
 
                            if (DateTime.Compare(currentDatetime, nextDatetime) > 0)
                            {
                                object[] hold;
                                hold = sourceData[i];
                                sourceData[i] = sourceData[i + 1];
                                sourceData[i + 1] = hold;
                            }

                            break;
                    }
                }
            }

            return sourceData;
        }

 

        /// <summary>
        /// 反轉資料排序
        /// </summary>
        /// <param>資料來源</param>
        /// <returns>反轉排序後的資料</returns>
        private object[][] ReverseArray(object[][] sourceData)
        {
            System.Collections.Stack tempStack = new System.Collections.Stack();

            foreach (object[] record in sourceData)
            {
                tempStack.Push(record);
            }

            for (int i = 0; i < sourceData.Length; i++)
            {
                sourceData[i] = (object[])tempStack.Pop();
            }

            return sourceData;
        }

        private void dgv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dgv.Rows.Count < 1)
                return;

            if (dbData.Length < 2)
                return;

            if (dgvColumnIndex != e.ColumnIndex)
            {
                dbData = Sorting(dbData, e.ColumnIndex);
                dgvColumnIndex = e.ColumnIndex;
            }
            else
            {
                dbData = ReverseArray(dbData);
            }
 
            DataToFillDgv();
        }

        /// <summary>
        /// 由buffer取出資料填入dbDataGridView
        /// </summary>
        private void DataToFillDgv()
        {
            dgv.Rows.Clear();
            int rowCount = 0;

            if (dgvCurrentPage % 10 == 0)
                rowCount = 10;
            else
                rowCount = dgvCurrentPage % 10;

            int startRow = (rowCount - 1) * dgvRowNumPerPage;
            int ListdbdbDataendrow = rowCount * dgvRowNumPerPage;

            if (dgvCurrentPage > 10 )
            {
                startRow = dgvCurrentPage * dgvRowNumPerPage - dgvRowNumPerPage;
                ListdbdbDataendrow = dgvCurrentPage * dgvRowNumPerPage;
            }

            int cnt2 = 0;

            for (int cnt = startRow; cnt < ListdbdbDataendrow; cnt++)
            {
                if (dbData.Length - 1 >= cnt)
                {
                    dgv.Rows.Add(dbData[cnt]);
                    cnt2++;
                }
                else
                {
                    break;
                }
            }

            if (dgvCurrentPage > 1 && dgv.Rows.Count < 1)
            {
                //重設,因為最後一頁沒東西了,要回到第一頁
                //iTemRefreshList(1);

                return;
            }

            LinkLabel firstLinkLabel = pagingPanel.Controls.Find("firstLinkLabel", true)[0] as LinkLabel;
            LinkLabel previousTenLinkLabel = pagingPanel.Controls.Find("previousTenLinkLabel", true)[0] as LinkLabel;
            LinkLabel afterTenLinkLabel = pagingPanel.Controls.Find("afterTenLinkLabel", true)[0] as LinkLabel;
            LinkLabel lastLinkLabel = pagingPanel.Controls.Find("lastLinkLabel", true)[0] as LinkLabel;

            firstLinkLabel.Enabled = true;
            previousTenLinkLabel.Enabled = true;
            afterTenLinkLabel.Enabled = true;
            lastLinkLabel.Enabled = true;

            if (dgvCurrentPage <= 10)
            {
                previousTenLinkLabel.Enabled = false;

                if (dgvCurrentPage == 1)
                {
                    firstLinkLabel.Enabled = false;
                }
            }

            if (dgvCurrentPage == dgvTotalPage)
            {
                //notice_lbl.Text += " 最後一頁 ";
                afterTenLinkLabel.Enabled = false;
                lastLinkLabel.Enabled = false;
            }

            if ((dgvTotalPage - dgvCurrentPage) < (dgvTotalPage % 10))
            {
                //notice_lbl.Text += " 最後10頁 ";
                afterTenLinkLabel.Enabled = false;
            }

            if (dgvTotalPage % 10 == 0 && (dgvTotalPage - dgvCurrentPage) < 10)
            {
                //notice_lbl.Text += " 最後10頁 ";
                afterTenLinkLabel.Enabled = false;
            }

            if (dgvTotalPage == 0)
            {
                //notice_lbl.Text += " 沒有頁數 ";
                lastLinkLabel.Enabled = false;
                firstLinkLabel.Enabled = false;
                previousTenLinkLabel.Enabled = false;
                afterTenLinkLabel.Enabled = false;
            }
        }

        /// <summary>
        /// 點選頁數按鈕時的事件
        /// </summary>
        /// <param></param>
        /// <param></param>
        private void Page_LinkLabelClick(object sender, EventArgs e)
        {
            if (dgv.Rows.Count < 1)
                return;

            int startNo = 0;
            int endNo = 0;
            bool setPage = false;

            LinkLabel pageLinkLabel = pagingPanel.Controls.Find(((LinkLabel)sender).Name, true)[0] as LinkLabel;
            LinkLabel firstLinkLabel = pagingPanel.Controls.Find("firstLinkLabel", true)[0] as LinkLabel;
            LinkLabel previousTenLinkLabel = pagingPanel.Controls.Find("previousTenLinkLabel", true)[0] as LinkLabel;
            LinkLabel afterTenLinkLabel = pagingPanel.Controls.Find("afterTenLinkLabel", true)[0] as LinkLabel;
            LinkLabel lastLinkLabel = pagingPanel.Controls.Find("lastLinkLabel", true)[0] as LinkLabel;

            switch (pageLinkLabel.Name)
            {
                case "firstLinkLabel": //第一
                    {
                        startNo = 1;
                        dgvCurrentPage = startNo;

                        if (startNo + 9 > dgvTotalPage)
                            endNo = dgvTotalPage;
                        else
                            endNo = startNo + 9;

                        if (dgvTotalPage > 10)
                        {
                            setPage = true;
                            CreateOtherLinkLabel();
                        }
                    }

                    break;

                case "lastLinkLabel": //最後
                    {
                        if (dgvTotalPage % 10 == 0)
                            startNo = (dgvTotalPage / 10 - 1) * 10 + 1;
                        else
                            startNo = (dgvTotalPage / 10) * 10 + 1;

                        endNo = dgvTotalPage;
                        dgvCurrentPage = dgvTotalPage;

                        if (dgvTotalPage > 10)
                        {
                            setPage = true;
                            CreateOtherLinkLabel();
                        }
                    }

                    break;

                case "previousTenLinkLabel":  //前10
                    {
                        if (dgvCurrentPage % 10 == 0)
                            startNo = dgvCurrentPage - 19;
                        else
                            startNo = (dgvCurrentPage / 10 - 1) * 10 + 1;

                        setPage = true;
                        endNo = startNo + 9;
                        dgvCurrentPage = startNo;
                        CreateOtherLinkLabel();
                    }

                    break;

                case "afterTenLinkLabel":  //後10
                    {
                        if (dgvCurrentPage % 10 == 0)
                            startNo = dgvCurrentPage + 1;
                        else
                            startNo = (dgvCurrentPage / 10 + 1) * 10 + 1;

                        setPage = true;
                        endNo = startNo + 9;
                        dgvCurrentPage = startNo;
                        CreateOtherLinkLabel();
                    }

                    break;

                default:
                    {
                        dgvCurrentPage = Convert.ToInt32(((LinkLabel)sender).Text);
                    }

                    break;
            }

            if (endNo >= dgvTotalPage)
            {
                endNo = dgvTotalPage;
            }

            if (setPage)
            {
                LinkLabel pager;
                int increPosition = 1;
                int pointX = 102;

                for (int pcount = startNo; pcount <= endNo; pcount++)
                {
                    pager = new LinkLabel();
                    pager.Name = "_" + pcount;
                    pager.Text = pcount.ToString();
                    pager.AutoSize = true;
                    pager.Size = new System.Drawing.Size(20, 10);
                    pager.Location = new System.Drawing.Point(pointX, 5);
                    pager.Click += new EventHandler(Page_LinkLabelClick);
                    pager.LinkColor = System.Drawing.Color.Black;
                    pagingPanel.Controls.Add(pager);
                    pointX += pager.Width + 10;
                    increPosition++;
                }
            }

            //設定目前頁碼為紅色
            LinkLabel click = pagingPanel.Controls.Find("_" + dgvCurrentPage, true)[0] as LinkLabel;
            click.LinkColor = System.Drawing.Color.Red;

            //改變先前頁碼, 從紅色改成黑色
            if (beforeLink != null && setPage != true)
            {
                Control[] conponemt = pagingPanel.Controls.Find(beforeLink, true);

                //若更改每頁顯示筆數, 有可能會有抓不到link button問題, 因此加以判斷不能為零
                if (conponemt.Length != 0)
                {
                    LinkLabel beforeclick = conponemt[0] as LinkLabel;
                    beforeclick.LinkColor = System.Drawing.Color.Black;
                }
            }

            //紀錄之前link button
            beforeLink = "_" + dgvCurrentPage;

            //讀取該頁面資料
            DataToFillDgv();
        }

        /// <summary>
        /// 建立後十頁.前十頁.第一頁.最後一頁按鈕
        /// </summary>
        private void CreateOtherLinkLabel()
        {
            pagingPanel.Controls.Clear();

            LinkLabel firstLinkLabel = new LinkLabel();
            firstLinkLabel.Name = "firstLinkLabel";
            firstLinkLabel.Text = "第一頁";
            firstLinkLabel.Size = new System.Drawing.Size(45, 20);
            firstLinkLabel.Location = new System.Drawing.Point(0, 5);
            firstLinkLabel.ForeColor = System.Drawing.Color.Black;
            firstLinkLabel.LinkColor = System.Drawing.Color.Black;
            firstLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            pagingPanel.Controls.Add(firstLinkLabel);

            LinkLabel previousTenLinkLabel = new LinkLabel();
            previousTenLinkLabel.Name = "previousTenLinkLabel";
            previousTenLinkLabel.Text = "前十頁";
            previousTenLinkLabel.Size = new System.Drawing.Size(50, 20);
            previousTenLinkLabel.ForeColor = System.Drawing.Color.Black;
            previousTenLinkLabel.Location = new System.Drawing.Point(50, 5);
            previousTenLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            previousTenLinkLabel.LinkColor = System.Drawing.Color.Black;
            pagingPanel.Controls.Add(previousTenLinkLabel);

            LinkLabel afterTenLinkLabel = new LinkLabel();
            afterTenLinkLabel.Name = "afterTenLinkLabel";
            afterTenLinkLabel.Text = "後十頁";
            afterTenLinkLabel.ForeColor = System.Drawing.Color.Black;
            afterTenLinkLabel.Size = new System.Drawing.Size(50, 20);
            afterTenLinkLabel.LinkColor = System.Drawing.Color.Black;

            if (dgvCurrentPage > 10)//假如目前頁碼大於10, 必須調整"後十頁"BUTTON位置, 因為頁碼有可能會蓋到"後十頁"BUTTON
                afterTenLinkLabel.Location = new System.Drawing.Point(460, 5);
            else
                afterTenLinkLabel.Location = new System.Drawing.Point(328, 5);

            afterTenLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            pagingPanel.Controls.Add(afterTenLinkLabel);

            LinkLabel lastLinkLabel = new LinkLabel();
            lastLinkLabel.Name = "lastLinkLabel";
            lastLinkLabel.Text = "最後一頁";
            lastLinkLabel.Size = new System.Drawing.Size(80, 20);
            lastLinkLabel.ForeColor = System.Drawing.Color.Black;

            if (dgvCurrentPage > 10)//假如目前頁碼大於10, 必須調整"最後一頁"BUTTON位置, 因為頁碼有可能會蓋到"最後一頁"BUTTON
                lastLinkLabel.Location = new System.Drawing.Point(510, 5);
            else
                lastLinkLabel.Location = new System.Drawing.Point(378, 5);

            lastLinkLabel.LinkColor = System.Drawing.Color.Black;
            lastLinkLabel.Click += new EventHandler(Page_LinkLabelClick);
            pagingPanel.Controls.Add(lastLinkLabel);

            Label promptLabel_1 = new Label();
            promptLabel_1.Text = "每頁顯示資料筆數:";
            promptLabel_1.AutoSize = true;
            promptLabel_1.Location = new System.Drawing.Point(600, 5);
            promptLabel_1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((int)(((byte)(62)))));
            pagingPanel.Controls.Add(promptLabel_1);

            TextBox promptTextBox = new TextBox();
            promptTextBox.Name = "promptTextBox";
            promptTextBox.Location = new System.Drawing.Point(717, 2);
            promptTextBox.Size = new System.Drawing.Size(40, 15);
            promptTextBox.Text = dgvRowNumPerPage.ToString();
            promptTextBox.KeyDown += new KeyEventHandler(TextKeyDown_num);
            promptTextBox.KeyDown += new KeyEventHandler(promptTextBox_KeyDown);
            pagingPanel.Controls.Add(promptTextBox);

            Label promptLabel_2 = new Label();
            promptLabel_2.Text = "上限50筆";
            promptLabel_2.AutoSize = true;
            promptLabel_2.Location = new System.Drawing.Point(770, 5);
            promptLabel_2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((int)(((byte)(62)))));
            pagingPanel.Controls.Add(promptLabel_2);
        }
    }
}

.Designer.cs


{
    partial class Form1
    {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 設計工具產生的程式碼

        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
        ///
        /// </summary>
        private void InitializeComponent()
        {
            this.pagingPanel = new System.Windows.Forms.Panel();
            this.dgv = new System.Windows.Forms.DataGridView();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dgv)).BeginInit();
            this.SuspendLayout();
            // 
            // pnl_paging
            // 
            this.pagingPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pagingPanel.Location = new System.Drawing.Point(0, 505);
            this.pagingPanel.Margin = new System.Windows.Forms.Padding(3, 6, 3, 3);
            this.pagingPanel.Name = "pagingPanel";
            this.pagingPanel.Padding = new System.Windows.Forms.Padding(0, 5, 0, 0);
            this.pagingPanel.Size = new System.Drawing.Size(761, 20);
            this.pagingPanel.TabIndex = 0;
            // 
            // dgv
            // 
            this.dgv.AllowUserToAddRows = false;
            this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1});
            this.dgv.Location = new System.Drawing.Point(12, 12);
            this.dgv.Name = "dgv";
            this.dgv.RowTemplate.Height = 24;
            this.dgv.Size = new System.Drawing.Size(733, 456);
            this.dgv.TabIndex = 0;
            this.dgv.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dgv_ColumnHeaderMouseClick);
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Column1";
            this.Column1.Name = "Column1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(761, 525);
            this.Controls.Add(this.dgv);
            this.Controls.Add(this.pagingPanel);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgv)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dgv;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.Panel pagingPanel;
    }
}