DataGridView实现分页

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.Collections;



namespace XjxMobilenursing

{

    public partial class PagerCtrl : UserControl

    {

        public PagerCtrl()

        {

            InitializeComponent();

        }

        [Browsable(true)]

        [Category("页面记录数")]

        [Description("显示页面记录数")]

        [DefaultValue(typeof(int), "10")]

        public int PageSize{ get;set; }

        public Hashtable condition { get; set; }//查询条件

        private int totalCount = 0;//总记录数

        public int TotalCount

        {

            get { return totalCount; }

            set 

            {

                totalCount = value;

                if (totalCount <= 0) totalCount = 0;

                if (totalCount <= PageSize)

                {

                    lkbNextPage.Enabled = false;

                }

                else

                {

                    lkbNextPage.Enabled = true;

                }

                lkbPrePage.Enabled = false;

                lblPageSize.Text = PageSize.ToString();

                lblTotalCount.Text = totalCount.ToString();

                lblPageNum.Text = PageNum.ToString();

            }

        }

        public int BeginPage//起始记录

        {

            get 

            {

                return (currentPage - 1) * PageSize + 1; 

            }

        }

        public int EndPage { //结束记录

            get {

                if ((CurrentPage < PageNum) || TotalCount % PageSize == 0)

                    {

                        return BeginPage + PageSize - 1;

                    }

                return BeginPage + (TotalCount % PageSize) - 1;

             }

        }



        private int currentPage = 1;//当前第几页

        public int CurrentPage

        {

            get { return currentPage; }

            set 

            {

                currentPage = value;

                lblCurrentPage.Text = currentPage.ToString();

            }

        }



        public int PageNum

        {

            get 

            { 

                int pageNum = 0;//总的页数

                if (PageSize > 0)

                {

                    pageNum = (totalCount % PageSize == 0) ? (totalCount / PageSize) : (totalCount / PageSize) + 1;

                }

                return pageNum;

            }

        }

        public delegate void LoadDataSouceDelegate(Hashtable condition);

        public event LoadDataSouceDelegate LoadDataSouce;

        private void lkbFirstPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {//首页

            CurrentPage = 1;

            LoadDataSouce(condition);

            lkbPrePage.Enabled = false;

            if (TotalCount > PageSize)

            {

                lkbNextPage.Enabled = true;

            }

        }



        private void lkbPrePage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {//上一页

            CurrentPage--;

            LoadDataSouce(condition);

            lkbNextPage.Enabled = true;

            if (CurrentPage <= 1)

            {

                CurrentPage = 1;

                lkbPrePage.Enabled = false;

            }

            else

            {

                lkbPrePage.Enabled = true;

            }

        }



        private void lkbNextPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {//下一页

            CurrentPage++;

            LoadDataSouce(condition);

            lkbPrePage.Enabled = true;

            if (CurrentPage < PageNum)

            {

                lkbNextPage.Enabled = true;

            }

            else

            {

                CurrentPage = PageNum;

                lkbNextPage.Enabled = false;

            }

        }



        private void lkbLastPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {//尾页

            CurrentPage = PageNum;

            LoadDataSouce(condition);

            lkbNextPage.Enabled = false;

            if (TotalCount > PageSize)

            {

                lkbPrePage.Enabled = true;

            }

        }



        private void btnGo_Click(object sender, EventArgs e)

        {//跳转

            if (!StringCommon.IsNumeric(txtWherePage.Text.Trim()))

            {

                MessageBox.Show("请输入数字");

                return;

            }

            int goWherePage = StringCommon.ToInt32Value(txtWherePage.Text.Trim(),1);

            if (goWherePage <= 0 || goWherePage > PageNum)

            {

                MessageBox.Show(string.Format("请输入1~{0}之间的数",PageNum)); 

                return;

            }

            CurrentPage = goWherePage;

            LoadDataSouce(condition);

            if (CurrentPage <= 1)

            {

                lkbPrePage.Enabled = false;

            }

            else

            {

                lkbPrePage.Enabled = true;

            }

            if (CurrentPage < PageNum)

            {

                lkbNextPage.Enabled = true;

            }

            else

            {

                lkbNextPage.Enabled = false;

            }

        }

    }

}

你可能感兴趣的:(datagridview)