Windows Form 分页 具体实现
using system;
using system.collections.generic;
using system.componentmodel;
using system.drawing;
using system.data;
using system.linq;
using system.text;
using system.windows.forms;
using system.data.common;
using system.collections;
namespace common
{
public partial class winformpager : usercontrol
{
public event eventhandler pagechanged; //事件:控件的当前页码发生变更。
private int pagesize;
private int curpage;
private int pagecount;
public winformpager()
{
initializecomponent();
}
private void winformpager_load(object sender, eventargs e)
{
}
/// <summary>
/// [属性]每页显示记录数。
/// </summary>
public int pagesize
{
get
{
if (pagesize <= 0)
{
pagesize = 10;
}
return pagesize;
}
set
{
pagesize = value;
}
}
/// <summary>
/// 当前页数
/// </summary>
public int curpage
{
get
{
if (curpage <= 0)
{
curpage = 1;
}
return curpage;
}
set
{
curpage = value;
if (pagechanged != null)
{
saferaise.raise(pagechanged,null);//触发当件页码变更事件。
}
}
}
/// <summary>
/// [属性]总页数。
/// </summary>
public int pagecount
{
get
{
if (recordcount > 0)
{
int pagecount = recordcount / pagesize;
if (recordcount % pagesize == 0)
{
pagecount = recordcount / pagesize;
}
else
{
pagecount = recordcount / pagesize + 1;
}
return pagecount;
}
else
{
return 0;
}
}
set
{
pagecount = value;
}
}
/// <summary>
/// [属性]总记录数。
/// </summary>
public int recordcount
{
get;
set;
}
/// <summary>
/// [属性]相对于当前页的上一页
/// </summary>
public int prevpage
{
get
{
if (curpage > 1)
{
return curpage - 1;
}
return 1;
}
}
/// <summary>
/// [属性]相对于当前页的下一页
/// </summary>
public int nextpage
{
get
{
if (curpage < pagecount)
{
return curpage + 1;
}
return pagecount;
}
}
private void btnfirstpage_click(object sender, eventargs e)
{
this.curpage = 1;
}
private void btnlastpage_click(object sender, eventargs e)
{
this.curpage = this.prevpage;
}
private void btnnextpage_click(object sender, eventargs e)
{
this.curpage = this.nextpage;
}
private void btnendpage_click(object sender, eventargs e)
{
this.curpage = this.pagecount;
}
private void txtpagenumber_textchanged(object sender, eventargs e)
{
if (!validator.isnumeric(this.txtpagenumber.text.trim()))
{
messagebox.show("请输入数字!");
}
}
private void btnjump_click(object sender, eventargs e)
{
if (!validator.isnumeric(this.txtpagenumber.text.trim()))//验证输入是否为数字
{
messagebox.show("请输入数字!");
}
else
{
if (int.parse(this.txtpagenumber.text.trim()) > 0)
{
if (int.parse(this.txtpagenumber.text.trim()) < this.pagecount)
{
this.curpage = int.parse(this.txtpagenumber.text.trim());
}
else
{
this.curpage = this.pagecount;
}
}
else
{
this.curpage = 1;
}
}
}
}
}
该用户自定义控件在页面中取名pager
private void binddata()
{
int rowcount = 0;
pager.pagesize = 15; datagridview.datasource = getlist(pager.curpage, pager.pagesize, out rowcount);
pager.recordcount = rowcount;
pager.lbnumber.text = string.format("共{0}条记录,每页{1}条记录,共{2}页", pager.recordcount.tostring(), pager.pagesize.tostring(), pager.pagecount.tostring());
}
private void pager_pagechanged(object sender, eventargs e)
{
binddata(); //重新对datagridview控件的数据源进行绑定。
}
控件
上一篇: ASP.NET操作各类时间段获取方法汇总
下一篇: c#获取季度时间实例代码(季度的第一天)