winform里dataGridView分页,默认dataGridView是不分页的和webform里不一样,webform中GridView自带自带了分页。

现在c/s的程序很多时候也需要webform的分页样式,所以就写了下面的分页,使用了access数据库。

原理就是读取数据存入datatable中,然后根据不同的页来显示当页数据。这样有个缺点就是数据量太大时会影响显示速度。sql server数据库时可以借助数据库来实现只读取当页数据来显示,效率会高一些。

所用环境:vs.net2010   access2003

form中控件如下图:

控件分别是:dataGridView1,button1,button3,button2,button4,textBox1,button5,label2,label3,label4,label5,label6,label7

分页效果如下图:

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace test
{
public partial class Form3 : Form
{
public int pageSize = ; //每页记录数
public int recordCount = ; //总记录数
public int pageCount = ; //总页数
public int currentPage = ; //当前页 public DataTable dtSource = new DataTable(); public Form3()
{
InitializeComponent();
} private void Form3_Load(object sender, EventArgs e)
{
//数据库操作获得DataTable
string sql = "select ID,title from info"; DB db = new DB(); dtSource = db.GetDt(sql);
// recordCount = dtSource.Rows.Count; pageCount = (recordCount / pageSize); if ((recordCount % pageSize) > )
{
pageCount++;
} //默认第一页
currentPage = ; LoadPage();
} private void LoadPage()
{
//
if (currentPage < ) currentPage = ;
if (currentPage > pageCount) currentPage = pageCount;
// int beginRecord;
int endRecord;
DataTable dtTemp; dtTemp = dtSource.Clone(); beginRecord = pageSize * (currentPage - );
if (currentPage == ) beginRecord = ;
endRecord = pageSize * currentPage;
if (currentPage == pageCount) endRecord = recordCount; for (int i = beginRecord; i < endRecord; i++)
{
dtTemp.ImportRow(dtSource.Rows[i]);
} dataGridView1.DataSource = dtTemp; label3.Text = currentPage.ToString();
label5.Text = pageCount.ToString();
label7.Text = recordCount.ToString();
textBox1.Text = currentPage.ToString();
} private void button1_Click(object sender, EventArgs e)
{
currentPage = ;
LoadPage();
}
private void button2_Click(object sender, EventArgs e)
{
currentPage++;
LoadPage();
}
private void button3_Click(object sender, EventArgs e)
{
currentPage--;
LoadPage();
}
private void button4_Click(object sender, EventArgs e)
{
currentPage = pageCount;
LoadPage();
} private void button5_Click(object sender, EventArgs e)
{
int pageN = Convert.ToInt32(textBox1.Text);
currentPage = pageN;
LoadPage();
} }
}

数据库access2003

winform里dataGridView分页代码,access数据库的更多相关文章

  1. (转)将access数据库迁移到SQLserver的两种方法

    在实际项目使用中遇到的问题,将原文整理后以备后用. 原文地址(具体链接几次未知):http://www.jb51.net/article/41956.htm 方法1 使用ACCESS2007自带的数据 ...

  2. 将ACCESS数据库迁移到SQLSERVER数据库

    原文:将ACCESS数据库迁移到SQLSERVER数据库 将ACCESS数据库迁移到SQLSERVER数据库 ACCESS2000文件 用ACCESS2007打开,并迁移到SQLSERVER2005里 ...

  3. C#读写Access数据库、表格datagridview窗体显示代码实例

    C#读写Access数据库.表格datagridview窗体显示代码实例 最近项目中用到C#对于Access数据库表读写.mdb操作,学习了下相关的东西,这里先整理C#对于Access数据库的操作,对 ...

  4. WinForm DataGridView分页功能

    WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件  .CS: 1 using System; ...

  5. winform中DataGridView实现分页功能

    WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载)   转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...

  6. winform连接ACCESS数据库

    1.先建立一个名叫text.mdb的access数据库 2.他它复制到access中文件下. 3.在App_Code文件夹下建好封装语句,查询方法,连接语句,其中studentDA中的代码为: pri ...

  7. 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现

    关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...

  8. access数据库及其分页的方法

    首先access数据库的话,感觉针对比较小型的网站比较适合.携带方便,不需要按照特定的sql环境. 当然如果使用access数据库的话 1.首先你先要下载办公五合一(access也是其中之一) 2.w ...

  9. thinkphp从数据库里的html代码显示页面不解析

    首先,这个问题不应该出现在这里,因为以前在用ThinkPHP3.1.2的时候,利用富文本编辑器保存文本后,直接从数据库里面取出的数据都能正常显示,改用ThinkPHP3.2.3之后,thinkphp从 ...

随机推荐

  1. Search Insert Position [LeetCode]

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. MySQL drop、delete和truncate的区别

    注意:这里说的delete是指不带where子句的delete语句 相同点 truncate和不带where子句的delete, 以及drop都会删除表内的数据 不同点: 1. truncate和 d ...

  3. 读<jquery 权威指南>[3]-动画

    一. 显示与隐藏——hide(),show() 1. 方法: hide(speed,[callback]); show(speed,[callback]); 说明:这两个方法还可以实现带动画效果的显示 ...

  4. MYSQL57密码策略修改

    1.查看当前的密码测试 show variables like 'validate_password%';

  5. [转]深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)

    以下内容转自: 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-l ...

  6. C语言文法的理解

    <程序> ->  <外部声明> |  <程序>  <外部声明> <外部声明>  ->   <函数定义>  |  &l ...

  7. Solr整合中文分词组件IKAnalyzer

    我用的Solr是4.10版本, 在csdn下载这个版本的IKAnalyzer:IK Analyzer 2012FF_hf1.zip 解压后目录如下: (1)这里还用solr自带的example实验分词 ...

  8. 第1章 Express MongoDB 搭建多人博客

    学习环境 Node.js : 0.10.22 + Express : 3.4.4 + MongoDB : 2.4.8 + 快速开始 安装 Express express 是 Node.js 上最流行的 ...

  9. cookie窃取和session劫持

    Updates 2014-08-17 感谢@搞前端的crosser的提醒,加入了HTTP Response Splitting的内容. 此篇文章的Presentation戳这里. 一.cookie的基 ...

  10. JS三元

    ((productDatas[i].Img.indexOf("http") == -1) ? ("/upload/190-160/" + productData ...