DataGridView 分页功能

 

 public partial class FrmDGV : Form
    {
        int pageSize = 0;         //每页显示行数
        int nMax = 0;                //总记录数
        int pageCount = 0;      //页数=总记录数/每页显示行数
        int pageCurrent = 0;   //当前页号
        int nCurrent = 0;        //当前记录行

        DataSet ds = new DataSet();   //定义一个DataSet
        System.Data.DataTable dtInfo = new System.Data.DataTable();   //定义一个DataTable

        public FrmDGV()
        {
            InitializeComponent();
            dgvInfo.Columns[0].HeaderCell.Value = "第一列";

            dgvInfo.TopLeftHeaderCell.Value = "Test";
            dgvInfo.ColumnHeadersHeight = 60; //表头高度
            dgvInfo.Columns[2].HeaderCell.Style.BackColor = Color.Red;//表头背景色
            dgvInfo.Columns[3].HeaderCell.Style.ForeColor = Color.Blue;//表头字体颜色


             dgvInfo.Rows[2].HeaderCell.Style.BackColor = Color.Red;//表头背景色
            dgvInfo.Rows[3].HeaderCell.Style.ForeColor = Color.Blue;//表头字体颜色


 
            OracleConnection Con = new OracleConnection("server=TRMESPP;user id=DIV_APP;password=DIV12APP;");
            Con.Open();
            string strSql = "Select *  From tb_ref_bcrprintinfo where yyyymmdd>to_char(sysdate,'yyyymmdd')";
            OracleDataAdapter sda = new OracleDataAdapter(strSql, Con);
            sda.Fill(ds, "ds");
            Con.Close();
            dtInfo = ds.Tables[0];
            InitDataSet();
        }

        private void InitDataSet()
        {
            pageSize = 35;      //设置页面行数
            nMax = dtInfo.Rows.Count;

            pageCount = (nMax / pageSize);    //计算出总页数

            if ((nMax % pageSize) > 0) pageCount++;

            pageCurrent = 1;    //当前页数从1开始
            nCurrent = 0;       //当前记录数从0开始

            LoadData();
        }

        private void LoadData()
        {
            int nStartPos = 0;                                //当前页面开始记录行
            int nEndPos = 0;                                 //当前页面结束记录行

            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架

            if (pageCurrent == pageCount)
                nEndPos = nMax;
            else
                nEndPos = pageSize * pageCurrent;

            nStartPos = nCurrent;

            lblPageCount.Text = pageCount.ToString();
            txtCurrentPage.Text = Convert.ToString(pageCurrent);

            //从元数据源复制记录行
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtInfo.Rows[i]);
                nCurrent++;
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            dgvInfo.DataSource = bdsInfo;
        }

        private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "关闭")
            {
                this.Close();
            }
            if (e.ClickedItem.Text == "上一页")
            {
                pageCurrent--;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }

                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                pageCurrent++;
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
        }

  
        //上一页
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            pageCurrent--;
            if (pageCurrent <= 0)
            {
                MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                return;
            }
            else
            {
                nCurrent = pageSize * (pageCurrent - 1);
            }

            LoadData();
        }


        //下一页
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            pageCurrent++;
            if (pageCurrent > pageCount)
            {
                MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
                return;
            }
            else
            {
                nCurrent = pageSize * (pageCurrent - 1);
            }
            LoadData();
        }


        //关闭
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值