[Visual Basic 2003~2005開發秘訣與範例大全]datagridview分頁

  • 3095
  • 0

[Visual Basic 2003~2005開發秘訣與範例大全]datagridview分頁

一樣參考書籍後改寫原本專案的程式

目的:

希望將datagridview顯示的資料分頁

修改前

clip_image002

修改後

clip_image004

作法:

原來作法是將所有資料查完後直接顯示在datagridview


        {
            string SqlCmd2 = @"SELECT DATAKEY, LCDATE, VLDATE, EXPIRY, NAL, BENEF, CURCDDESCECHI,substr(TXAMT,1,Length(TXAMT)-2)||'.'||substr(TXAMT,Length(TXAMT)-1,2) as TXAMT
            from (select * from LC left outer join CURCD  on LC.CURCD = CURCD.CURCDNO) as c order by DATAKEY";
            dtSource = NewMediaTest1.Model.DbOperation.SelectQuery(SqlCmd2);
            this.dataGridView_LC.DataSource = dtSource;
        }

修改如下

1. 先在頁面中拉好控制項(修改後)

2. 宣告下列變數在form底下


    {
        /*定義分頁資料所需變數*/
        private int pageCount;//最大頁數
        private int maxRec;//最後一筆資料數目
        private int pageSize;//每頁筆數
        private int currentPage;//目前頁數
        private int recNo;//目前資料數目
        private DataTable dtSource = new DataTable();//來源資料

3. 增加分頁顯示及判斷的程式碼


        /// <summary>
        /// 檢查是否有輸入每頁筆數
        /// </summary>
        /// <returns></returns>
        private bool CheckFillButton()
        {
            int result;
            if (int.TryParse(txtPageSize.Text, out result) == false || result == 0)
            {
                MessageBox.Show("請輸入正確的每頁筆數!不得為零!");
                txtPageSize.Focus();
                return false;
            }
            else
            {
                pageSize = result;
                return true;
            }
        }
        /// <summary>
        /// 顯示頁面資訊
        /// </summary>
        private void DisplayPageInfo()
        {
            txtNumberDisplay.Text = "頁次:" + currentPage.ToString() + "/ " + pageCount.ToString();
        }
        /// <summary>
        /// 更新datagridview分頁後的內容
        /// </summary>
        private void LoadPage()
        {
            int startRec, endRec;
            DataTable dtTemp;            
            //只複製結構(淺複製)
            dtTemp = dtSource.Clone();
            endRec = (currentPage == pageCount) ? maxRec : pageSize * currentPage;
            startRec = recNo;
            for (int i = startRec; i <endRec ; i++)
            {
                dtTemp.ImportRow(dtSource.Rows[i]);
                recNo += 1;
            }
            this.dataGridView_LC.DataSource = dtTemp;
            DisplayPageInfo();
        }
        /// <summary>
        /// 初始頁面資料
        /// </summary>
        private void FillDataGridView()
        {
            RefreshDataGridViewLc();
            if (CheckFillButton() == false)
            {
                return;
            }
            maxRec = dtSource.Rows.Count;
            pageCount = maxRec / pageSize;
            pageCount += (maxRec % pageSize > 0) ? 1 : 0;
            currentPage = 1;
            recNo = 0;
            LoadPage();
        }
        #endregion

4. 頁面點選的判斷


        private void btnSetPageDisplay_Click(object sender, EventArgs e)
        {
            FillDataGridView();
        }
        //第一頁
        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            if (CheckFillButton() == false)
            {
                return;
            }
            else if (currentPage == 1)
            {
                MessageBox.Show("您已經到達第一頁!");
                return;
            }
            else
            {
                currentPage = 1;
                recNo = 0;
                LoadPage();
            }
        }
        //前一頁
        private void btnPreviousPage_Click(object sender, EventArgs e)
        {
            if (CheckFillButton() == false)
            {
                return;
            }
            if (currentPage == pageCount)
            {
                recNo = pageSize * (currentPage - 2);
            }
            currentPage -= 1;
            if (currentPage < 1)
            {
                MessageBox.Show("您已經到達第一頁!");
                currentPage = 1;
                return;
            }
            else
            {
                recNo = pageSize * (currentPage - 1);
            }
            LoadPage();
        }
        //下一頁
        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if (CheckFillButton() == false)
            {
                return;
            }
            currentPage += 1;
            if (currentPage > pageCount)
            {
                currentPage = pageCount;
                if (recNo == maxRec)
                {
                    MessageBox.Show("您已到達最後一頁!");
                    return;
                }
            }
            LoadPage();
        }
        //最後一頁
        private void btnFinalPage_Click(object sender, EventArgs e)
        {
            if (CheckFillButton() == false)
            {
                return;
            }
            if (recNo == maxRec)
            {
                MessageBox.Show("您已到達最後一頁!");
                return;
            }
            currentPage = pageCount;
            recNo = pageSize * (currentPage - 1);
            LoadPage();
        }

這樣就完成了

備註:

可以將分頁顯示及判斷的程式碼獨立成一個類別,並用觀察者模式和視窗(form)溝通,有空再來改寫。

參考資料

Visual Basic 2003~2005開發秘訣與範例大全