码迷,mamicode.com
首页 > Windows程序 > 详细

C# 翻页设计:首页,上一页,下一页,末页 ,跳转

时间:2014-11-24 14:55:00      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:datagridview   blog   http   ar   os   sp   for   数据   on   

bubuko.com,布布扣
 int pageSize = 0;     //每页显示行数
        int nMax = 0;         //总记录数
        int pageCount = 0;    //页数=总记录数/每页显示行数
        int pageCurrent = 0;   //当前页号
        int nCurrent = 0;      //当前记录行       
private void Bind()
        {
            //td = um.dataAll();
            pageSize = 30;      //设置页面行数
            nMax = td.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 = td.Clone();   //克隆DataTable结构框架

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

            nStartPos = nCurrent;
            lblPageCount.Text = "共 " + pageCount.ToString() + " 页";
            txtCurrentPage.Text = Convert.ToString(pageCurrent);


            //从元数据源复制记录行
            if (td.Rows.Count > 0)
            {
                for (int i = nStartPos; i < nEndPos; i++)
                {
                    dtTemp.ImportRow(td.Rows[i]);
                    nCurrent++;
                }
            }
           
            dataGridView1.DataSource = dtTemp;
            //datagridView默认不选中行
           
            int endC = td.Rows.Count;
            if (endC > pageSize * pageCurrent)

                endC = pageSize * pageCurrent;
            int k = 0;

            for (int i = pageSize * (pageCurrent - 1) + 1; i <= endC; i++)
            {


                dataGridView1.Rows[k].HeaderCell.Value = i.ToString();
                k++;
            }
           
           
        }
private void up_Click(object sender, EventArgs e)
        {
            pageCurrent--;
            if (pageCurrent <= 0)
            {
                MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                return;
            }
            else
            {
                nCurrent = pageSize * (pageCurrent - 1);
            }
            LoadData();

        }

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

        }

        private void first_Click(object sender, EventArgs e)
        {
            pageCurrent = 1;
            nCurrent = pageSize * (pageCurrent - 1);
            LoadData();

        }

        private void end_Click(object sender, EventArgs e)
        {
            pageCurrent = pageCount;
            nCurrent = pageSize * (pageCurrent - 1);
            LoadData();
        }

        private void skip_Click(object sender, EventArgs e)
        {
            int CurrentP = Convert.ToInt32(txtCurrentPage.Text.ToString());
            if (CurrentP > pageCount)
            {
                MessageBox.Show("超过最大页数,请重新输入");
                return;
            }
            else
            {
                pageCurrent = CurrentP;
                nCurrent = pageSize * (pageCurrent - 1);
                LoadData();
            }
        }

 

C# 翻页设计:首页,上一页,下一页,末页 ,跳转

标签:datagridview   blog   http   ar   os   sp   for   数据   on   

原文地址:http://www.cnblogs.com/zzlp/p/4118514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!