c#在winform中用DataGridView实现分页效果

public partial class Form11 : Form
{
public Form11()
{
InitializeComponent();
}
private int Inum = 1;
//行号,规定其索引初始值为1
int pagesize = 4;
int allCount
=0;
int pagecount = 0;
private void
Form11_Load(object sender, EventArgs e)
{
using(SqlConnection connection = new
SqlConnection("server=数服务器名称;uid=sa;pwd=sa;database=数据库名"))
{
SqlDataAdapter da = new SqlDataAdapter("select * from
tb_Land", connection);
DataTable dt = new DataTable();
da.Fill(dt);
allCount = dt.Rows.Count;
//获取数据表中记录的个数
pagecount = allCount %
pagesize; //页数 = 总数/显示的行数
//只能显示一页,判断是否是整除
if (pagecount == 0)
{
pagecount =allCount / pagesize;
}
else
{
pagecount
= allCount / pagesize + 1;
}
this.label1.Text = "共" +
pagecount.ToString() + "页";
show(1,4);
//分页显示记录
// this.label2.Text = "当前第" + Inum.ToString() +
"页";
}
}
/// <summary>
///
分页显示数据表中的信息
/// </summary>
/// <param
name="start"></param>
/// <param
name="end"></param>
private void show(int start,int
end)
{
SqlConnection connection = new
SqlConnection("server=服务器名称;uid=sa;pwd=sa;database=数据库名称");
SqlDataAdapter das = new
SqlDataAdapter("select top "+pagesize+" * from
tb_Land where Id not in (select top "+pagesize*(Inum-1)+" Id from
tb_Land)",connection);
//sql语句
DataSet ds = new DataSet();
//显示指定范围的记录
das.Fill(ds,"one");
this.dataGridView1.DataSource =
ds.Tables["one"].DefaultView; //绑定显示数据
ds =
null;
}
//第一页
private void
linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
e)
{
Inum = 1;
show(Inum,
pagesize);
}
//末尾页
private void
linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
e)
{
Inum = pagecount;
show(Inum-1,
pagesize);
}
//下一页
private void
linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
e)
{
Inum++;
if (Inum <= pagesize)
//如果没有超出记录行数
{
show(Inum,
pagesize);
}
else
{
MessageBox.Show("现已是最后一页记录");
Inum =
pagecount-1; 更改为 Inum = pagecount;
return;
}
}
//上一页
private void
linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Inum--;
if (Inum >
0) //如果当前不是首记录
{
show(Inum, pagesize); //显示上一页记录
}
else
{
MessageBox.Show("现已是第一页记录");
Inum =
1;
return;
}
}
}
}
c#在winform中用DataGridView实现分页效果的更多相关文章
- winform中DataGridView实现分页功能
WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载) 转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...
- winform里dataGridView分页代码,access数据库
winform里dataGridView分页,默认dataGridView是不分页的和webform里不一样,webform中GridView自带自带了分页. 现在c/s的程序很多时候也需要webfo ...
- C# DataGridView自定义分页控件
好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...
- WinForm轻松实现自定义分页 (转载)
转载至http://xuzhihong1987.blog.163.com/blog/static/267315872011315114240140/ 以前都是做web开发,最近接触了下WinForm, ...
- thinkphp ajax 无刷新分页效果的实现
思路:先做出传统分页效果,然后重新复制一份Page.class.php类,对它进行修改,把js中的函数传到page类中,把上一页.下一页.首页.尾页.链接页中的url地址改成js控制的函数,模板页面中 ...
- WinForm Control - DataGridView
http://blog.csdn.net/fangxing80/article/details/1561011 .NET 2.0 - WinForm Control - DataGridView 编程 ...
- winfrom dataGridView 自定义分页实现
Winfrom 基本处于忘光的阶段.先需要做个winfrom 的软件.然后自己扩展了DataGridView带分页的控件.废话不多说 上图先 现在一步步实现其效果. 1.添加用户控件 上图即可知道 ...
- 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现
关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...
- winform 取消datagridview第一行选中状态
C# WinForm 取消DataGridView的默认选中Cell 使其不反蓝 http://www.cnblogs.com/freeliver54/archive/2009/02/16/13913 ...
随机推荐
- ETL__pentaho__SPOON_PDI
Pentaho Data Integration (PDI, also called Kettle),是pentaho的etl工具.虽然etl工具一般都用在数据仓库环境中,可是,PDI还是可以做以下事 ...
- Git学习-->如何通过Shell脚本自动定时将Gitlab备份文件复制到远程服务器?
一.背景 在我之前的博客 git学习--> Gitlab如何进行备份恢复与迁移? (地址:http://blog.csdn.net/ouyang_peng/article/details/770 ...
- python __init__ 构造函数
实例化过程 会执行__init__ 的函数方法 class SQLHelper: def __init__(self): # self = s1 print("helo") def ...
- Java-idea-FindBugs、PMD和CheckStyle对比
一.对比 工具 目的 检查项 备注 FindBugs 检查.class 基于Bug Patterns概念,查找javabytecode (.class文件)中的潜在bug 主要检查bytecode中的 ...
- Virtualbox中win7虚拟机中U盘不可用问题的解决
Virtualbox版本是5.0.0,主机运行多是Ubuntu12.04 LTS,虚拟机是Win7 X64.起初Win7正常运行,Virtualbox的增强功能已安装.下面是如何一步一步解决U盘不可用 ...
- 基于struts2框架-自定义身份证号验证器
自定义拦截器的步骤: 1.定义一个验证器的类: > 自定义的验证器都需要实现 Validator接口. > 可以选择继承 ValidatorSupport 或 FieldValidato ...
- Python中被双下划线包围的魔法方法
基本的魔法方法 __new__(cls[, ...]) 用来创建对象 1. __new__ 是在一个对象实例化的时候所调用的第一个方法 2. 它的第一个参数是这个类,其他的参数是用来直接传递给 __i ...
- VS2010/MFC编程入门之九(对话框:为控件添加消息处理函数)
创建对话框类和添加控件变量在上一讲中已经讲过,这一讲的主要内容是如何为控件添加消息处理函数. MFC为对话框和控件等定义了诸多消息,我们对它们操作时会触发消息,这些消息最终由消息处理函数处理.比如我们 ...
- springcloud12---sidecar
Sidecar:异构平台整合.做了一个桥 package com.itmuch.cloud; import org.springframework.boot.SpringApplication; im ...
- 探索C++虚函数在g++中的实现
本文是我在追查一个诡异core问题的过程中收获的一点心得,把公司项目相关的背景和特定条件去掉后,仅取其中通用的C++虚函数实现部分知识记录于此. 在开始之前,原谅我先借用一张图黑一下C++: “无敌” ...