分页控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace HospitalInfoManage { public partial class PageControl : UserControl { public PageControl() { InitializeComponent(); } #region 分页字段和属性 private int pageIndex = 1; /// <summary> /// 当前页面 /// </summary> public virtual int PageIndex { get { return pageIndex; } set { pageIndex = value; } } private int pageSize = 100; /// <summary> /// 每页记录数 /// </summary> public virtual int PageSize { get { return pageSize; } set { pageSize = value; } } private int recordCount = 0; /// <summary> /// 总记录数 /// </summary> public virtual int RecordCount { get { return recordCount; } set { recordCount = value; } } private int pageCount = 1; /// <summary> /// 总页数 /// </summary> public int PageCount { get { if (pageSize != 0) { pageCount = GetPageCount(); if (pageCount==0) { //pageIndex = 0; pageCount = 1; } } return pageCount; } } private string jumpText; /// <summary> /// 跳转按钮文本 /// </summary> public string JumpText { get { if (string.IsNullOrEmpty(jumpText)) { jumpText = "Go"; } return jumpText; } set { jumpText = value; } } #endregion #region 页码变化触发事件 public event EventHandler OnPageChanged; public event EventHandler OnOutExcel; #endregion #region 分页及相关事件功能实现 private void SetFormCtrEnabled() { btnFirst.Enabled = true; btnPrev.Enabled = true; btnNext.Enabled = true; btnLast.Enabled = true; btnGo.Enabled = true; } /// <summary> /// 计算总页数 /// </summary> /// <returns></returns> private int GetPageCount() { if (PageSize == 0) { return 0; } int pageCount = RecordCount / PageSize; if (RecordCount % PageSize == 0) { pageCount = RecordCount / PageSize; } else { pageCount = RecordCount / PageSize + 1; } //if (pageCount<PageIndex) //{ // PageIndex = 1; // DrawControl(true); //} return pageCount; } /// <summary> /// 外部调用 /// </summary> public void DrawControl(int count) { recordCount = count; DrawControl(false); } /// <summary> /// 页面控件呈现 /// </summary> private void DrawControl(bool callEvent) { if (callEvent && OnPageChanged != null) { OnPageChanged(this, null);//当前分页数字改变时,触发委托事件 } btnGo.Text = JumpText; txtPageCount.Text = PageCount.ToString(); txtPageIndex.Text = PageIndex.ToString(); txtPageCount.Size = new Size(txtPageCount.Text.Length * 20, 20); txtPageIndex.Size = new Size(txtPageIndex.Text.Length * 20, 20); lblDataCount.Text = string.Format("共{0}条记录", RecordCount.ToString()); lblSize.Text = string.Format("每页{0}条记录", PageSize.ToString()); SetFormCtrEnabled(); if (PageCount == 1 || PageCount == 0)//有且仅有一页 { btnFirst.Enabled = false; btnPrev.Enabled = false; btnNext.Enabled = false; btnLast.Enabled = false; btnGo.Enabled = false; } else if (PageIndex == 1)//第一页 { btnFirst.Enabled = false; btnPrev.Enabled = false; } else if (PageIndex == PageCount)//最后一页 { btnNext.Enabled = false; btnLast.Enabled = false; } } #endregion #region 相关控件事件 /// <summary> /// 第一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFirst_Click(object sender, EventArgs e) { PageIndex = 1; DrawControl(true); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrev_Click(object sender, EventArgs e) { PageIndex = Math.Max(1, PageIndex - 1); DrawControl(true); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNext_Click(object sender, EventArgs e) { PageIndex = Math.Min(PageCount, PageIndex + 1); DrawControl(true); } /// <summary> /// 最后一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLast_Click(object sender, EventArgs e) { PageIndex = PageCount; DrawControl(true); } /// <summary> /// enter键功能 /// </summary> private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e) { btnGo_Click(null, null); } /// <summary> /// 跳转页数限制 /// </summary> private void txtCurrentPage_TextChanged(object sender, EventArgs e) { int num = 0; if (int.TryParse(txtCurrentPage.Text.Trim(), out num) && num > 0) { if (num > PageCount) { txtCurrentPage.Text = PageCount.ToString(); } } } /// <summary> /// 跳转 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGo_Click(object sender, EventArgs e) { int num = 1; if (int.TryParse(txtCurrentPage.Text.Trim(), out num) && num > 0) { PageIndex = num; DrawControl(true); } } #endregion /// <summary> /// 每页显示数据量变更时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtPageSize_TextChanged(object sender, EventArgs e) { int num = 0; if (int.TryParse(txtPageSize.Text.Trim(), out num) && num > 0) { if (num > 10000) { txtPageSize.Text = "10000"; } } } /// <summary> /// 保存设置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButton1_Click(object sender, EventArgs e) { if (txtPageSize.Text.Trim().Length>0) { PageSize = int.Parse(txtPageSize.Text.Trim()); PageIndex = 1; DrawControl(true); } } /// <summary> /// 导出Excle /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButton2_Click(object sender, EventArgs e) { if (OnOutExcel!=null) { OnOutExcel(null, null); } } } }
设计样式: