Winform分页控件
设计界面:
控件代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 ClientModels; namespace XXPLClient.UI { public partial class PagerControl : UserControl { #region PageSize private int _pageSize = 10; /// <summary> /// 每页数据条数 /// </summary> public int PageSize { get { return _pageSize; } set { if (value == 0) { _pageSize = 1; Pager.rows = 1; } else { _pageSize = value; Pager.rows = value; } } } #endregion #region 事件 public event PageChangedHandler PageChanged = null; public event RefreshDataHandler RefreshData = null; #endregion #region Pager private PagerModel _pager = new PagerModel(1, 10); public PagerModel Pager { get { return _pager; } set { _pager = value; txtCurrentPage.Text = _pager.page.ToString(); lblTotalPage.Text = " / " + _pager.pageCount.ToString(); } } #endregion #region PagerControl 构造函数 public PagerControl() { InitializeComponent(); } #endregion #region PagerControl_Load private void PagerControl_Load(object sender, EventArgs e) { tools.BackColor = SystemColors.Control; } #endregion private void btnFirst_Click(object sender, EventArgs e) { Pager.page = 1; if (PageChanged != null) { PageChanged(); } } private void btnPre_Click(object sender, EventArgs e) { Pager.page = Pager.prePage; if (PageChanged != null) { PageChanged(); } } private void btnNext_Click(object sender, EventArgs e) { Pager.page = Pager.nextPage; if (PageChanged != null) { PageChanged(); } } private void btnLast_Click(object sender, EventArgs e) { Pager.page = Pager.pageCount; if (PageChanged != null) { PageChanged(); } } private void btnRefresh_Click(object sender, EventArgs e) { if (RefreshData != null) { RefreshData(); } } private void btnCurrentPage_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar)) { e.Handled = true; } } private void txtCurrentPage_KeyUp(object sender, KeyEventArgs e) { if (Convert.ToInt32(txtCurrentPage.Text) < 1) { txtCurrentPage.Text = "1"; } if (Convert.ToInt32(txtCurrentPage.Text) > Pager.pageCount) { txtCurrentPage.Text = Pager.pageCount.ToString(); } Pager.page = Convert.ToInt32(txtCurrentPage.Text); if (PageChanged != null) { PageChanged(); } } } /// <summary> /// 翻页 /// </summary> public delegate void PageChangedHandler(); /// <summary> /// 刷新数据 /// </summary> public delegate void RefreshDataHandler(); }
PagerModel代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; namespace ClientModels { /// <summary> /// 分页 /// </summary> [Serializable] public class PagerModel { #region 字段 /// <summary> /// 当前页数 /// </summary> public int page { get; set; } /// <summary> /// 每页记录数 /// </summary> public int rows { get; set; } /// <summary> /// 排序字段 /// </summary> public string sort { get; set; } /// <summary> /// 排序的方式asc,desc /// </summary> public string order { get; set; } /// <summary> /// 记录 /// </summary> public object result { get; set; } /// <summary> /// 记录数 /// </summary> public int totalRows { get; set; } #endregion #region 构造函数 public PagerModel() { } /// <summary> /// /// </summary> /// <param name="page">当前页数</param> /// <param name="rows">每页记录数</param> public PagerModel(int page, int rows) { this.page = page; this.rows = rows; } #endregion #region 扩展字段 /// <summary> /// 总页数 /// </summary> public int pageCount { get { if (rows != 0) { return (totalRows - 1) / rows + 1; } else { return 0; } } } /// <summary> /// 上一页 /// </summary> public int prePage { get { if (page - 1 > 0) { return page - 1; } return 1; } } /// <summary> /// 下一页 /// </summary> public int nextPage { get { if (page + 1 < pageCount) { return page + 1; } return pageCount; } } #endregion } }
如何使用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PagerModel pager = pagerControl.Pager; List<BS_Meeting_ext> list; if (cbxReviewTime.Checked) { list = m_MeetingDal.GetList(ref pager, cbxType.SelectedValue.ToString(), txtTerm.Text, txtNum.Text, dtpStart.Value.ToString("yyyy-MM-dd"), dtpEnd.Value.ToString("yyyy-MM-dd")); } else { list = m_MeetingDal.GetList(ref pager, cbxType.SelectedValue.ToString(), txtTerm.Text, txtNum.Text, null, null); } pagerControl.Pager = pager;
使用效果图: