using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProjectBase.Data.Pager;
namespace ProjectBase.Controls
{
public delegate void PageChangedEventHandler(object sender, EventArgs e);
public delegate void ExportCurrentEventHandler(object sender, EventArgs e);
public delegate void ExportAllEventHandler(object sender, EventArgs e);
///
/// 分页工具条用户控件,仅提供分页信息显示及改变页码操作
///
public partial class Pager : DevExpress.XtraEditors.XtraUserControl
{
///
/// 页面切换的时候触发的事件
///
public event PageChangedEventHandler PageChanged;
public event ExportCurrentEventHandler ExportCurrent;
public event ExportAllEventHandler ExportAll;
private int m_PageSize;
private int m_PageCount;
private int m_RecordCount;
private int m_CurrentPageIndex;
private bool m_ShowExportButton = true;//是否显示导出按钮
private PagerInfo pagerInfo = null;
///
/// 分页信息
///
public PagerInfo PagerInfo
{
get
{
if (pagerInfo == null)
{
pagerInfo = new PagerInfo();
pagerInfo.RecordCount = this.RecordCount;
pagerInfo.CurrenetPageIndex = this.CurrentPageIndex;
pagerInfo.PageSize = this.PageSize;
pagerInfo.OnPageInfoChanged += new PageInfoChanged(pagerInfo_OnPageInfoChanged);
}
else
{
pagerInfo.CurrenetPageIndex = this.CurrentPageIndex;
}
return pagerInfo;
}
}
void pagerInfo_OnPageInfoChanged(PagerInfo info)
{
this.RecordCount = info.RecordCount;
this.CurrentPageIndex = info.CurrenetPageIndex;
this.PageSize = info.PageSize;
this.InitPageInfo();
}
///
/// 必需的设计器变量。在Designer中
///
//private System.ComponentModel.Container components = null;
///
/// 默认构造函数,设置分页初始信息
///
public Pager()
{
InitializeComponent();
this.m_PageSize = 50;
this.m_RecordCount = 0;
this.m_CurrentPageIndex = 1; //默认为第一页
this.InitPageInfo();
}
///
/// 带参数的构造函数
/// 每页记录数
/// 总记录数
///
public Pager(int recordCount, int pageSize)
{
InitializeComponent();
this.m_PageSize = pageSize;
this.m_RecordCount = recordCount;
this.m_CurrentPageIndex = 1; //默认为第一页
this.InitPageInfo();
}
///
/// 清理所有正在使用的资源。在Designer中
///
//protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// if (components != null)
// {
// components.Dispose();
// }
// }
// base.Dispose(disposing);
//}
///
/// 页面变化处理
///
///
protected virtual void OnPageChanged(EventArgs e)
{
if (PageChanged != null)
{
PageChanged(this, e);
}
}
///
/// 是否显示导出按钮
///
[Description("是否显示导出按钮。"), DefaultValue(true), Category("分页")]
public bool ShowExportButton
{
get { return m_ShowExportButton; }
set
{
m_ShowExportButton = value;
this.btn_Export.Visible = value;
this.btn_ExportCurrent.Visible = value;
}
}
///
/// 设置或获取一页中显示的记录数目
///
[Description("设置或获取一页中显示的记录数目"), DefaultValue(50), Category("分页")]
public int PageSize
{
set
{
this.m_PageSize = value;
}
get
{
return this.m_PageSize;
}
}
///
/// 获取记录总页数
///
[Description("获取记录总页数"), DefaultValue(0), Category("分页")]
public int PageCount
{
get
{
return this.m_PageCount;
}
}
///
/// 设置或获取记录总数
///
[Description("设置或获取记录总数"), Category("分页")]
public int RecordCount
{
set
{
this.m_RecordCount = value;
}
get
{
return this.m_RecordCount;
}
}
///
/// 当前的页面索引, 开始为1
///
[Description("当前的页面索引, 开始为1"), DefaultValue(0), Category("分页")]
[Browsable(false)]
public int CurrentPageIndex
{
set
{
this.m_CurrentPageIndex = value;
}
get
{
return this.m_CurrentPageIndex;
}
}
///
/// 初始化分页信息
///
///
public void InitPageInfo(PagerInfo info)
{
this.m_RecordCount = info.RecordCount;
this.m_PageSize = info.PageSize;
this.InitPageInfo();
}
///
/// 初始化分页信息
/// 每页记录数
/// 总记录数
///
public void InitPageInfo(int recordCount, int pageSize)
{
this.m_RecordCount = recordCount;
this.m_PageSize = pageSize;
this.InitPageInfo();
}
///
/// 初始化分页信息
/// 总记录数
///
public void InitPageInfo(int recordCount)
{
this.m_RecordCount = recordCount;
this.InitPageInfo();
}
///
/// 初始化分页信息
///
public void InitPageInfo()
{
if (this.m_PageSize < 1)
this.m_PageSize = 10; //如果每页记录数不正确,即更改为10
if (this.m_RecordCount < 0)
this.m_RecordCount = 0; //如果记录总数不正确,即更改为0
//取得总页数
if (this.m_RecordCount % this.m_PageSize == 0)
{
this.m_PageCount = this.m_RecordCount / this.m_PageSize;
}
else
{
this.m_PageCount = this.m_RecordCount / this.m_PageSize + 1;
}
//设置当前页
if (this.m_CurrentPageIndex > this.m_PageCount)
{
this.m_CurrentPageIndex = this.m_PageCount;
}
if (this.m_CurrentPageIndex < 1)
{
this.m_CurrentPageIndex = 1;
}
//设置按钮的可用性
bool enable = (this.CurrentPageIndex > 1);
this.btn_Previous.Enabled = enable;
enable = (this.CurrentPageIndex < this.PageCount);
this.btn_Next.Enabled = enable;
this.txt_CurrentPage.Text = this.m_CurrentPageIndex.ToString();
this.label_PageInfo.Text = string.Format("共 {0} 条记录,每页 {1} 条,共 {2} 页", this.m_RecordCount, this.m_PageSize, this.m_PageCount);
this.btn_Export.Enabled = this.ShowExportButton;
this.btn_ExportCurrent.Enabled = this.ShowExportButton;
}
///
/// 刷新页面数据
///
/// 页码
public void RefreshData(int page)
{
this.m_CurrentPageIndex = page;
EventArgs e = new EventArgs();
OnPageChanged(e);
}
private void btn_First_Click(object sender, EventArgs e)
{
this.RefreshData(1);
}
private void btn_Previous_Click(object sender, EventArgs e)
{
if (this.m_CurrentPageIndex > 1)
{
this.RefreshData(this.m_CurrentPageIndex - 1);
}
else
{
this.RefreshData(1);
}
}
private void btn_Next_Click(object sender, EventArgs e)
{
if (this.m_CurrentPageIndex < this.m_PageCount)
{
this.RefreshData(this.m_CurrentPageIndex + 1);
}
else if (this.m_PageCount < 1)
{
this.RefreshData(1);
}
else
{
this.RefreshData(this.m_PageCount);
}
}
private void btn_Last_Click(object sender, EventArgs e)
{
if (this.m_PageCount > 0)
{
this.RefreshData(this.m_PageCount);
}
else
{
this.RefreshData(1);
}
}
private void txt_CurrentPage_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int num;
try
{
num = Convert.ToInt16(this.txt_CurrentPage.Text);
}
catch
{
num = 1;
}
if (num > this.m_PageCount)
num = this.m_PageCount;
if (num < 1)
num = 1;
this.RefreshData(num);
}
}
private void btn_ExportCurrent_Click(object sender, EventArgs e)
{
if (ExportCurrent != null)
{
ExportCurrent(sender, e);
}
}
private void btn_Export_Click(object sender, EventArgs e)
{
if (ExportAll != null)
{
ExportAll(sender, e);
}
}
}
}