winform分頁的製作

成品效果:



下面是效果實現的所有代碼:

備註:

控件名稱解釋:

首頁(按鈕)-->btnHomePage

上一頁(按鈕)-->btnUpPage

1(按鈕)-->btnShowYe1

2(按鈕)-->btnShowYe2

3(按鈕)-->btnShowYe3

下一頁(按鈕)-->btnNextPage

尾頁(按鈕)-->btnEnd

43(lable)-->lblSumPage

文本框-->txtPage

確定(按鈕)-->btnQueding


        #region 分頁
 
        int pageSize = 2;//每頁顯示的條數
        int currentPage = 1;//當前頁碼
 
        /// <summary>
        /// 分頁
        /// </summary>
        /// <param name="pageNo"></param>
        private void FenYe(int pageNo)
        {
            //把最大頁碼傳給總共的頁數lable
            lblSumPage.Text = getTotalPage().ToString();
            ds = new DataSet();
            string sql = "select top " + pageSize + " * from Administrator_Log where ID not in (";
            sql += "select top (" + (pageNo - 1+ " *" + pageSize + ") ID from Administrator_Log order by ID)order by ID";
            adapter = new SqlDataAdapter(sqlDBhelp.conn);
            if (ds.Tables["Administrator_Log"!= null)
            {
                ds.Tables["Administrator_Log"].Clear();
            }
            adapter.Fill(ds"Administrator_Log");
 
            DataView dv = new DataView(ds.Tables["Administrator_Log"]);
            this.dgvLog.DataSource = dv;
        }
        #endregion
 
        #region 跳轉到首頁
        /// <summary>
        /// 跳轉到首頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHomePage_Click(object senderEventArgs e)
        {
            SheZhiBtn(123);
            SheZhiBtn(falsetruetrue);
            currentPage = 1;
            FenYe(currentPage);
 
        }
        #endregion  跳轉到首頁
 
        #region 上一頁
        /// <summary>
        /// 上一頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpPage_Click(object senderEventArgs e)
        {
            currentPage--;
            if (currentPage <= 1)
            {
                currentPage = 1;
                SheZhiBtn(123);
                SheZhiBtn(falsetruetrue);
                FenYe(currentPage); ;
                MessageBox.Show("已是第一頁!");
                return;
            }
            SheZhiBtn(currentPage - 1currentPagecurrentPage + 1);
            SheZhiBtn(truefalsetrue);
            FenYe(currentPage);
        }
        #endregion
 
        #region 下一頁
        /// <summary>
        /// 下一頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNextPage_Click(object senderEventArgs e)
        {
            currentPage++;
            //判斷當前頁碼是否超過最大頁碼
            if (currentPage > getTotalPage())
            {
                SheZhiBtn(currentPage - 3currentPage - 2currentPage - 1);
                SheZhiBtn(truetruefalse);
                currentPage = getTotalPage();
                MessageBox.Show("已是最大頁碼");
                return;
            }
            //SheZhiBtn()
            SheZhiBtn(currentPage - 1currentPagecurrentPage + 1);
            SheZhiBtn(truefalsetrue);
            FenYe(currentPage);
        }
        #endregion
 
        #region 設置指定頁按鈕的構造方法
        private void SheZhiBtn(int oneint twoint three)
        {
            btnShowYe1.Text = one.ToString();
            btnShowYe2.Text = two.ToString();
            btnShowYe3.Text = three.ToString();
        }
 
        private void SheZhiBtn(bool onebool twobool three)
        {
 
            btnShowYe1.Enabled = one;
            btnShowYe2.Enabled = two;
            btnShowYe3.Enabled = three;
            //if (!btnShowYe1.Enabled)
            //{
            //    btnShowYe1.BackColor = Color.Black;
            //    btnShowYe1.ForeColor = Color.White;
            //}
            //else if (!btnShowYe2.Enabled)
            //{
            //    btnShowYe2.BackColor = Color.Black;
            //    btnShowYe2.ForeColor = Color.White;
            //}
            //else 
            //{
            //    btnShowYe3.BackColor = Color.Black;
            //    btnShowYe3.ForeColor = Color.White;
            //}
        }
        #endregion
 
        #region 跳轉到指定頁數
        /// <summary>
        /// 跳轉到第一頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShowYe1_Click(object senderEventArgs e)
        {
            currentPage = Convert.ToInt32(((Button)sender).Text);
            FenYe(currentPage);
            //判斷不是第一頁和最後一頁
            if (currentPage != 1 && currentPage != getTotalPage())
            {
                if (!((Button)sender).Equals(btnShowYe2))
                {
                    SheZhiBtn(currentPage - 1currentPagecurrentPage + 1);
                }
                else
                {
                    SheZhiBtn(truefalsetrue);
                }
            }
            else if (currentPage == 1)
            {
                SheZhiBtn(falsetruetrue);
            }
            else
            {
                SheZhiBtn(truetruefalse);
            }
        }
        #endregion
 
        #region 跳轉到尾頁
        /// <summary>
        /// 跳轉到尾頁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnd_Click(object senderEventArgs e)
        {
            SheZhiBtn((getTotalPage() - 2), (getTotalPage() - 1), getTotalPage());
            SheZhiBtn(truetruefalse);
            currentPage = getTotalPage();
            FenYe(currentPage);
 
 
        }
        #endregion
 
        #region 查詢當前最大的頁碼是多少
        /// <summary>
        /// 查詢當前最大的頁碼是多少
        /// </summary>
        /// <returns>返回最大的頁碼</returns>
        public int getTotalPage()
        {
            string sql = "select  count(*) from Administrator_Log";
            int count = DBhelp.countList(sql);
 
            //判斷總數據除以每頁顯示的條數的餘數是否爲0
            if (count % pageSize == 0)
            {
                //如果爲0,用總數據除以每頁顯示的條數,返回回去
                return count / pageSize;
            }
            else
            {
                //如果不爲0,用總數除以每頁顯示的條數再+1
                return count / pageSize + 1;
            }
        }
        #endregion
 
        #region 點擊確定按鈕跳轉到指定頁面
        /// <summary>
        /// 點擊確定按鈕跳轉到指定頁面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueding_Click(object senderEventArgs e)
        {
            if (!txtPage.Text.Trim().Equals(string.Empty))
            {
                int page = Convert.ToInt32(txtPage.Text.Trim());
                if (page > 1 && page < getTotalPage())
                {
                    currentPage = page;
                    SheZhiBtn(currentPage - 1currentPagecurrentPage + 1);
                    SheZhiBtn(truefalsetrue);
                    FenYe(currentPage);
                }
                else if (page <= 1)
                {
                    SheZhiBtn(123);
                    SheZhiBtn(falsetruetrue);
                    currentPage = 1;
                    FenYe(currentPage);
                }
                else
                {
                    SheZhiBtn((getTotalPage() - 2), (getTotalPage() - 1), getTotalPage());
                    SheZhiBtn(truetruefalse);
                    currentPage = getTotalPage();
                    FenYe(currentPage);
                }
            }
            else
            {
                MessageBox.Show("請輸入指定的頁數");
            }
            txtPage.Text = string.Empty;
        }
        #endregion
 
        #region 輸入跳轉指定頁面文本框的限制
 
        /// <summary>
        /// 輸入跳轉指定頁面文本框的限制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPage_KeyPress(object senderKeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar&& e.KeyChar != 8)
            {
                e.Handled = true;
            }
        }
        #endregion
發佈了25 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章