您的位置:首页 > 编程语言

一个DataGridView的分页代码

2008-06-23 01:44 344 查看
//from:http://www.ohuo.net/?p=83

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace Jav_Tool
{
public class ClsDataGridViewPage
{
private int _RowsPerPage; //每页记录数
private int _TotalPage; //总页数
private int _curPage = 0; //当前页数
private DataGridView _DataGridView; //要分页的DataGridView
private DataView _dv; //与需要分页显示的的DataView
public int RowsPerPage { get { return _RowsPerPage; } set { _RowsPerPage = value; } } //获取与设置每页记录数
public int TotalPage { get { return _TotalPage; } } //获取总页数
public int curPage { get { return _curPage; } set { _curPage = value; } } //获取与设置当前页数
public DataGridView GetDataGridView { set { _DataGridView = value; } } //设置需要分页的GetDataGridView
public DataView SetDataView { set { _dv = value; } }
public ClsDataGridViewPage()
{

}
public ClsDataGridViewPage(DataGridView datagridview, DataView dv, int RowsPerPage)
{
_DataGridView = datagridview;
_dv = dv;
_RowsPerPage = RowsPerPage;
}
public void Paging() //开始分页啦
{ //首先判断DataView中的记录数是否足够形成多页,
//如果不能,那么就只有一页,且DataGridView需要显示的记录等同于“最后一页”的记录
if (_dv.Count <= _RowsPerPage)
{
_TotalPage = 1;
GoLastPage();
return;
}
if (_dv.Count % _RowsPerPage == 0)
{
_TotalPage = (int)(_dv.Count / _RowsPerPage);
}
else
{
_TotalPage = (int)(_dv.Count / _RowsPerPage) + 1;
}
GoFirstPage();
}
//到第一页
public void GoFirstPage()
{
if (_TotalPage == 1)
{
GoLastPage();
return;
}
_curPage = 0;
GoNoPage(_curPage);
}
public void GoNextPage()
{
_curPage += 1;
if (_curPage > _TotalPage - 1)
{
_curPage = _TotalPage - 1;
return;
}
if (_curPage == _TotalPage - 1)
{
GoLastPage();
return;
}
GoNoPage(_curPage);
}
public void GoPrevPage()
{//’防止不合法的当前页号
_curPage -= 1;
if (_curPage < 0)
{
_curPage = 0;
return;
}
}
//到最后一页
public void GoLastPage()
{
_curPage = _TotalPage - 1;
int i;
DataTable dt;
dt = _dv.ToTable().Clone();
for (i = (_TotalPage - 1) * _RowsPerPage; i <= _dv.Count - 1; i++)
{
DataRow dr = dt.NewRow();
dr.ItemArray = _dv.ToTable().Rows[i].ItemArray;
dt.Rows.Add(dr);
}
_DataGridView.DataSource = dt;
}
public void GoNoPage(int PageNo)
{
_curPage = PageNo;
if (_curPage < 0)
{//防止不合法的页号
return;
}
//防止页号溢出
if (_curPage >= _TotalPage)
{ //页号超出上限
return;
}
if (_curPage == _TotalPage - 1)
{
//如果页号是最后一页,就显示最后一页
GoLastPage();
return;
}
DataTable dt;
dt = _dv.ToTable().Clone();
int i;
for (i = PageNo * _RowsPerPage; i <= (PageNo + 1) * _RowsPerPage - 1; i++)
{
DataRow dr = dt.NewRow();
dr.ItemArray = _dv.ToTable().Rows[i].ItemArray;
dt.Rows.Add(dr);
}
_DataGridView.DataSource = dt;
}
}
}

注意:要想让DataGridView分页显示记录,最关键的需要设置的分页类的三个属性是:
SetDataGridView 该属性用于设置窗体上要分页显示记录的DataGridView控件
RowsPerPage 该属性用来设置每页需要显示的记录数
SetDataView 该属性用来设置需要在DataGridView空间上显示的DataView
可以灵活地设置这三个属性,以满足不同的DataGridView对不同的DataView进行指定每页记录数的显示(是不是很方便?)现在举例如何设置这三个属性:
先获得数据源dt
DataTable dt = DataBiz.GetGmsystemReplyInfo(userid, out strErrmsg);
ClsDataGridViewPage dgvPage = new ClsDataGridViewPage();
dgvPage.GetDataGridView = this.dataGridView1; //需要分页的是 dataGridView1
dgvPage.RowsPerPage = 10; //每页显示10条记录
//获取需要分页显示的DataView
dgvPage.SetDataView = dt.DefaultView;
dgvPage.Paging(); //开始分页 调用分页类的Paging 方法

要看第一页,调用分页类的GoFirstPage方法
要看下一页,调用 GoNextPage方法
要看前一页,调用GoPrevPage方法
要看最后一页,调用GoLastPage方法
要看指定页号的页,调用 GoNoPage方法
只要灵活使用好了这个类,你的DataGridView控件的分页功能绝不成问题。而且随时都能了解当前到了第几页(curPage()+1),一共有多少页(TotalPage())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: