123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- 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);
- /// <summary>
- /// 分页工具条用户控件,仅提供分页信息显示及改变页码操作
- /// </summary>
- public partial class Pager : DevExpress.XtraEditors.XtraUserControl
- {
- /// <summary>
- /// 页面切换的时候触发的事件
- /// </summary>
- 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;
- /// <summary>
- /// 分页信息
- /// </summary>
- 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();
- }
- /// <summary>
- /// 必需的设计器变量。在Designer中
- /// </summary>
- //private System.ComponentModel.Container components = null;
- /// <summary>
- /// 默认构造函数,设置分页初始信息
- /// </summary>
- public Pager()
- {
- InitializeComponent();
- this.m_PageSize = 50;
- this.m_RecordCount = 0;
- this.m_CurrentPageIndex = 1; //默认为第一页
- this.InitPageInfo();
- }
- /// <summary>
- /// 带参数的构造函数
- /// <param name="pageSize">每页记录数</param>
- /// <param name="recordCount">总记录数</param>
- /// </summary>
- public Pager(int recordCount, int pageSize)
- {
- InitializeComponent();
- this.m_PageSize = pageSize;
- this.m_RecordCount = recordCount;
- this.m_CurrentPageIndex = 1; //默认为第一页
- this.InitPageInfo();
- }
- /// <summary>
- /// 清理所有正在使用的资源。在Designer中
- /// </summary>
- //protected override void Dispose(bool disposing)
- //{
- // if (disposing)
- // {
- // if (components != null)
- // {
- // components.Dispose();
- // }
- // }
- // base.Dispose(disposing);
- //}
- /// <summary>
- /// 页面变化处理
- /// </summary>
- /// <param name="e"></param>
- protected virtual void OnPageChanged(EventArgs e)
- {
- if (PageChanged != null)
- {
- PageChanged(this, e);
- }
- }
- /// <summary>
- /// 是否显示导出按钮
- /// </summary>
- [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;
- }
- }
- /// <summary>
- /// 设置或获取一页中显示的记录数目
- /// </summary>
- [Description("设置或获取一页中显示的记录数目"), DefaultValue(50), Category("分页")]
- public int PageSize
- {
- set
- {
- this.m_PageSize = value;
- }
- get
- {
- return this.m_PageSize;
- }
- }
- /// <summary>
- /// 获取记录总页数
- /// </summary>
- [Description("获取记录总页数"), DefaultValue(0), Category("分页")]
- public int PageCount
- {
- get
- {
- return this.m_PageCount;
- }
- }
- /// <summary>
- /// 设置或获取记录总数
- /// </summary>
- [Description("设置或获取记录总数"), Category("分页")]
- public int RecordCount
- {
- set
- {
- this.m_RecordCount = value;
- }
- get
- {
- return this.m_RecordCount;
- }
- }
- /// <summary>
- /// 当前的页面索引, 开始为1
- /// </summary>
- [Description("当前的页面索引, 开始为1"), DefaultValue(0), Category("分页")]
- [Browsable(false)]
- public int CurrentPageIndex
- {
- set
- {
- this.m_CurrentPageIndex = value;
- }
- get
- {
- return this.m_CurrentPageIndex;
- }
- }
- /// <summary>
- /// 初始化分页信息
- /// </summary>
- /// <param name="info"></param>
- public void InitPageInfo(PagerInfo info)
- {
- this.m_RecordCount = info.RecordCount;
- this.m_PageSize = info.PageSize;
- this.InitPageInfo();
- }
- /// <summary>
- /// 初始化分页信息
- /// <param name="pageSize">每页记录数</param>
- /// <param name="recordCount">总记录数</param>
- /// </summary>
- public void InitPageInfo(int recordCount, int pageSize)
- {
- this.m_RecordCount = recordCount;
- this.m_PageSize = pageSize;
- this.InitPageInfo();
- }
- /// <summary>
- /// 初始化分页信息
- /// <param name="recordCount">总记录数</param>
- /// </summary>
- public void InitPageInfo(int recordCount)
- {
- this.m_RecordCount = recordCount;
- this.InitPageInfo();
- }
- /// <summary>
- /// 初始化分页信息
- /// </summary>
- 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;
- }
- /// <summary>
- /// 刷新页面数据
- /// </summary>
- /// <param name="page">页码</param>
- 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);
- }
- }
- }
- }
|