Winform分页控件

设计界面:

Winform分页控件

控件代码:

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 = ;
/// <summary>
/// 每页数据条数
/// </summary>
public int PageSize
{
get
{
return _pageSize;
}
set
{
if (value == )
{
_pageSize = ;
Pager.rows = ;
}
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(, );
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 = ;
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) < )
{
txtCurrentPage.Text = "";
}
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代码:

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 != )
{
return (totalRows - ) / rows + ;
}
else
{
return ;
}
}
}
/// <summary>
/// 上一页
/// </summary>
public int prePage
{
get
{
if (page - > )
{
return page - ;
}
return ;
}
}
/// <summary>
/// 下一页
/// </summary>
public int nextPage
{
get
{
if (page + < pageCount)
{
return page + ;
}
return pageCount;
}
}
#endregion }
}

如何使用:

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;

使用效果图:

Winform分页控件

上一篇:开发框架模块视频系列(2)-Winform分页控件介绍


下一篇:winform基础控件总结