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 WinFormPag...
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)
{
}
///
/// [属性]每页显示记录数。
///
public int PageSize
{
get
{
if (pageSize 1)
{
return CurPage - 1;
}
return 1;
}
}
///
/// [属性]相对于当前页的下一页
///
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控件的数据源进行绑定。
}
控件