Windows Form 分页 具体实现

时间:2021-05-20

复制代码 代码如下:
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控件的数据源进行绑定。
}

控件

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章