DataGridView控件怎么实现分页功能?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataGridView控件怎么实现分页功能?相关的知识,希望对你有一定的参考价值。
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;
using System.Data.SqlClient;
namespace 数据库分页功能
public partial class Form1 : Form
public Form1()
InitializeComponent();
int i, start;//i为总行数,start为起始位置
int size = 4;//定义一个每页显示的行数
string str = @"Data Source=当前计算机名;Initial Catalog=ptwinshopmanage;Integrated Security=True";//连接字符串
string str1 = "select * from [HouseMessage]";//sql语句
private void Form1_Load(object sender, EventArgs e)
SqlConnection sqlcon = new SqlConnection(str);//连接数据库
sqlcon.Open();//打开数据库
SqlCommand sqlcmd = new SqlCommand(str1,sqlcon);//执行sql语句
SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);//数据适配器
DataTable dt = new DataTable();//创建表
sqlada.Fill(dt);//填充数据集
i = dt.Rows.Count;//总的行数或者记录数
show(0, size);//每页显示4条记录
start = 0;//第一行
sqlcon.Close();
//每页显示记录数j-i+1条
public void show(int i, int j)
SqlConnection sqlcon = new SqlConnection(str);
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
sqlada.Fill(ds, i, j, "[HouseMessage]");//把数据库表中的i-j的记录数用来填充数据集
ListData.DataSource = ds.Tables["[HouseMessage]"];//ListData为datagridview控件的name属性
ds = null;//清空数据集
//首页
private void button1_Click(object sender, EventArgs e)
ListData.DataSource = null;//清空数据
show(0,size);//调用show函数
//上一页
private void button2_Click(object sender, EventArgs e)
ListData.DataSource = null;
start = start - 1;
if (start < 1)
MessageBox.Show("已是第一页");
show(0,size);
else
show(start * size, size);
//下一页
private void button3_Click(object sender, EventArgs e)
ListData.DataSource = null;
start = start + 1;
show(start *size, size);
//尾页
private void button4_Click(object sender, EventArgs e)
ListData.DataSource = null;
show(i - size, i);
运行结果:
int pageSize = 0;
int nMax = 0;
int pageCount = 0;
int pageCurrent = 0;
int nCurrent = 0;
DataSet myDs = new DataSet();
DataTable dt = new DataTable();
public frmPage()
InitializeComponent();
FillData();
public void FillData()
SqlConnection conn = MyClass.CreateConn();
conn.Open();
SqlDataAdapter myDa = new SqlDataAdapter("select * from bookInfo", conn);
myDa.Fill(myDs, "myTable");
conn.Close();
dt = myDs.Tables[0];
InitDataSet();
private void InitDataSet()
pageSize = 10;
nMax = dt.Rows.Count;
pageCount = (nMax / pageSize);
if ((nMax%pageSize)>0)
pageCount++;
pageCurrent = 1;
nCurrent = 0;
LoadData();
private void LoadData()
int nStartPos = 0;
int nEndPos = 0;
DataTable dtTemp = dt.Clone();
if (pageCurrent==pageCount)
nEndPos = nMax;
else
nEndPos = pageSize * pageCurrent;
nStartPos = nCurrent;
toolStripLabel1.Text = "/ "+pageCount.ToString();
toolStripLabel2.Text = Convert.ToString(pageCurrent);
for (int i = nStartPos; i < nEndPos; i++)
dtTemp.ImportRow(dt.Rows[i]);
nCurrent++;
BindSource.DataSource=dtTemp;
bnPager.BindingSource = BindSource;
DGView.DataSource = BindSource;
private void bnPager_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
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();
参考资料: http://mengzhongjian.com/newsview.asp?id=55 参考技术C DataGridView有延时加载功能的,不用分页的。真要分页的话要自己写代码。 参考技术D 看它这种态度 ,大家都不要给它任何帮助!就算它给一千分,不要有人回答任何关于技术上的问题
以上是关于DataGridView控件怎么实现分页功能?的主要内容,如果未能解决你的问题,请参考以下文章