Windows Form 分页 Windows Form 分页 具体实现
人气:0using 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控件的数据源进行绑定。
}
控件
加载全部内容