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(); } }