Demo的界面
![](https://i-blog.csdnimg.cn/blog_migrate/74d292b1b9ba3e55030bd66fd591a868.jpeg)
我利用事件委托事件,仿http://www.cnblogs.com/huyong/写的公共分页用户控件
#region 版权信息
/*---------------------------------------------------------------------*
// 项目 名称:《Winform分页控件》
// 文 件 名: Pager.cs
// 描 述: 分页控件
// 作 者:kwon yan
*----------------------------------------------------------------------*/
#endregion
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;
namespace HuishengFS.Controls
{
/**/
/// <summary>
/// 申明委托
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public delegate int EventPagingHandler(EventPagingArg e);
/**/
/// <summary>
/// 分页控件呈现
/// </summary>
public partial class Pager : UserControl
{
public Pager()
{
InitializeComponent();
}
public event EventPagingHandler EventPaging;
/**/
/// <summary>
/// 每页显示记录数
/// </summary>
private int _pageSize = 50;
/**/
/// <summary>
/// 每页显示记录数
/// </summary>
public int PageSize
{
get { return _pageSize; }
set
{
_pageSize = value;
GetPageCount();
}
}
private int _nMax = 0;
/**/
/// <summary>
/// 总记录数
/// </summary>
public int NMax
{
get { return _nMax; }
set
{
_nMax = value;
GetPageCount();
}
}
private int _pageCount = 0;
/**/
/// <summary>
/// 页数=总记录数/每页显示记录数
/// </summary>
public int PageCount
{
get { return _pageCount; }
set { _pageCount = value; }
}
private int _pageCurrent = 0;
/**/
/// <summary>
/// 当前页号
/// </summary>
public int PageCurrent
{
get { return _pageCurrent; }
set { _pageCurrent = value; }
}
/// <summary>
/// 设置页面大小
/// </summary>
private void GetPageCount()
{
if (this.NMax > 0)
{
this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
lblPageCount.Text = " / " + PageCount.ToString();
//lblPageCount1.Text = "每页 "+PageSize .ToString ()+" 条,共 "+PageCount.ToString()+" 页";
lblPageCount1.Text = "Page no: " + PageSize.ToString() + ",Total:" + PageCount.ToString() + " pages";
}
else
{
this.PageCount = 0;
}
}
/**/
/// <summary>
/// 翻页控件数据绑定的方法 关键是这步,都是调用这里
/// </summary>
public void Bind()
{
if (this.EventPaging != null)
{
this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent));
}
if (this.PageCurrent > this.PageCount)
{
this.PageCurrent = this.PageCount;
}
if (this.PageCount == 1)
{
this.PageCurrent = 1;
}
lblcurentpage.Text = PageCurrent.ToString();
//lblRecordCount.Text = "共有 " + NMax.ToString() + " 条记录";
lblRecordCount.Text = "Total: " + NMax.ToString() + " records";
btnPrev.Enabled = true;
btnFirst.Enabled = true;
btnLast.Enabled = true;
btnNext.Enabled = true;
if (this.PageCurrent == 1)
{
this.btnPrev.Enabled = false;
this.btnFirst.Enabled = false;
}
if (this.PageCurrent == this.PageCount)
{
this.btnLast.Enabled = false;
this.btnNext.Enabled = false;
}
if (this.NMax == 0)
{
btnNext.Enabled = false;
btnLast.Enabled = false;
btnFirst.Enabled = false;
btnPrev.Enabled = false;
}
cmbPagecount.Items.Clear();
for (int i = 1; i <= PageCount; i++)
cmbPagecount.Items.Add(i.ToString());
cmbPagecount.SelectedIndex = PageCurrent - 1;
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirst_Click(object sender, EventArgs e)
{
PageCurrent = 1;
this.Bind();
}
//上一页
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrev_Click(object sender, EventArgs e)
{
PageCurrent -= 1;
if (PageCurrent <= 0)
{
PageCurrent = 1;
}
this.Bind();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
this.PageCurrent += 1;
if (PageCurrent > PageCount)
{
PageCurrent = PageCount;
}
this.Bind();
}
/// <summary>
/// 最后页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLast_Click(object sender, EventArgs e)
{
PageCurrent = PageCount;
this.Bind();
}
/// <summary>
/// 转到新页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btnGo_Click(object sender, EventArgs e)
{
if (Int32.TryParse(cmbPagecount.SelectedItem.ToString(), out _pageCurrent))
{
this.Bind();
}
}
}
/**/
/// <summary>
/// 自定义事件数据基类
/// </summary>
public class EventPagingArg : EventArgs
{
private int _intPageIndex;
public EventPagingArg(int PageIndex)
{
_intPageIndex = PageIndex;
}
}
}
前提准备
编写分页的SQL 语句和获取总数的SQL语句
--分页存储过程 也可以SQL语句--获取总数的语句 我也不必多写了
if exists(select * from sysobjects where name='Proc_DgvPage')
drop proc name='Proc_DgvPage')
go
create proc name='Proc_DgvPage')
(
@keyword varchar(100), --关键字
@start varchar(5),
@end varchar(5)
)
as
begin
select sortTb.clientCode, sortTb.fileReference,sortTb.clientName1, sortTb.clientName2,
sortTb.dateOpen, sortTb.dateClose,sortTb.fileMaster,
sortTb.clientTel, sortTb.clientMobile, sortTb.clientFex,sortTb.companyName,sortTb.clientAddr, sortTb.propFlat, sortTb.propFloor,
sortTb.propBlock, sortTb.propBuilding, sortTb.propStree, sortTb.propDistrict, sortTb.propArea, sortTb.isFileClosed
from (
select row_number() over(order by c.clientCode) as sortNo,
c.clientCode, f.fileSubpervisor+''/''+f.fileCode+''/''+f.fileYear+''/''+f.fileIntro+''/''+f.fileHandler as fileReference,
c.clientLastName1+'' ''+c.clientFirstName1 as clientName1, c.clientLastName2+'' ''+c.clientFirstName2 as clientName2,f.dateOpen, f.dateClose,f.fileMaster,
c.clientTel, c.clientMobile, c.clientFex,c.companyName,c.clientAddr, f.propFlat, f.propFloor,
f.propBlock, f.propBuilding, f.propStree, f.propDistrict, f.propArea, f.isFileClosed
from clientInfo c left join clientFiles f on c.clientCode = f.fileCode
where 1=1
)
as sortTb
where sortNo between @start and @end
end
go
在窗体调用:
//定义变量DataTable dtPage;
/// <summary>
/// GridViw数据绑定
/// </summary>
/// <returns></returns>
private int BindDgv()
{
//传入要取的第一条和最后一条
string start = (pager1.PageSize * (pager1.PageCurrent - 1) + 1).ToString();
string end = (pager1.PageSize * pager1.PageCurrent).ToString();
//数据源
dtPage = achieve.GetAll(Keyword, start, end);
//绑定分页控件
pager1.bindingSource1.DataSource = dtPage;
pager1.bindingNavigator1.BindingSource = pager1.bindingSource1;
//讲分页控件绑定DataGridView
dgvClients.DataSource = pager1.bindingSource1;
//返回总记录数
return achieve.GetToalCount(Keyword);
}
/// <summary>
/// 分页控件产生的事件
/// </summary>
private int pager1_EventPaging(HuishengFS.Controls.EventPagingArg e)
{
return DgvBind();
}
/// <summary>
/// 加载分页 或许写在Load事件里面
/// </summary>
private void FrmPage_Shown(object sender, EventArgs e)
{
#region DataGridView与Pager控件绑定
this.pager1.PageCurrent = 1;//当前页为第一页
pager1.PageSize = 100;//页数
this.pager1.Bind();//绑定
#endregion
}
- UcControl.rar (7.5 KB)