winform 分页控件
以前都是从事B/S开发,由于公司有个比较大的C/S项目,在使用DATAGRIDVIEW的时候,显示数据量比较大,所以才用分页模式,也不知道这样是否正确。
想个C/S下面的分页控件,都没有什么好的,就自己跟B/S下的分页控件,修改成WINFORM下面的。
首先创建一个用户控件名称为pager,在控件中拖入bindingNavigator和bindingSource,修改bindingNavigator,加入必要的一些控件。
效果如下:
代码实现如下:
namespace WindowsApp.MyControl
{
/**//// <summary>
/// 申明委托
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public delegate int EventPagingHandler(EventPagingArg e);
/**//// <summary>
/// 分页控件呈现
/// </summary>
public partial class Pager : UserControl
{
public Pager()
{
{
/**//// <summary>
/// 申明委托
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public delegate int EventPagingHandler(EventPagingArg e);
/**//// <summary>
/// 分页控件呈现
/// </summary>
public partial class Pager : UserControl
{
public Pager()
{
InitializeComponent();
}
public event EventPagingHandler EventPaging;
/**//// <summary>
/// 每页显示记录数
/// </summary>
private int _pageSize = 20;
/**//// <summary>
/// 每页显示记录数
/// </summary>
public int PageSize
{
get { return _pageSize; }
set
{
}
public event EventPagingHandler EventPaging;
/**//// <summary>
/// 每页显示记录数
/// </summary>
private int _pageSize = 20;
/**//// <summary>
/// 每页显示记录数
/// </summary>
public int PageSize
{
get { return _pageSize; }
set
{
_pageSize = value;
GetPageCount();
}
}
private int _nMax = 0;
/**//// <summary>
/// 总记录数
/// </summary>
public int NMax
{
get { return _nMax; }
set
{
_nMax = value;
GetPageCount();
}
}
private int _nMax = 0;
/**//// <summary>
/// 总记录数
/// </summary>
public int NMax
{
get { return _nMax; }
set
{
_nMax = value;
GetPageCount();
}
}
private int _pageCount = 0;
/**//// <summary>
/// 页数=总记录数/每页显示记录数
/// </summary>
public int PageCount
{
get { return _pageCount; }
set { _pageCount = value; }
}
private int _pageCurrent = 0;
}
}
private int _pageCount = 0;
/**//// <summary>
/// 页数=总记录数/每页显示记录数
/// </summary>
public int PageCount
{
get { return _pageCount; }
set { _pageCount = value; }
}
private int _pageCurrent = 0;
/**//// <summary>
/// 当前页号
/// </summary>
public int PageCurrent
{
get { return _pageCurrent; }
set { _pageCurrent = value; }
}
private void GetPageCount()
{
if (this.NMax > 0)
{
this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) /
/// 当前页号
/// </summary>
public int PageCurrent
{
get { return _pageCurrent; }
set { _pageCurrent = value; }
}
private void GetPageCount()
{
if (this.NMax > 0)
{
this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) /
Convert.ToDouble(this.PageSize)));
}
else
{
this.PageCount = 0;
}
}
/**//// <summary>
/// 翻页控件数据绑定的方法
/// </summary>
public void Bind()
{
if (this.EventPaging != null)
{
}
else
{
this.PageCount = 0;
}
}
/**//// <summary>
/// 翻页控件数据绑定的方法
/// </summary>
public void Bind()
{
if (this.EventPaging != null)
{
this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent));
winform增删改查简单案例 }
if (this.PageCurrent > this.PageCount)
{
this.PageCurrent = this.PageCount;
}
if (this.PageCount == 1)
{
this.PageCurrent = 1;
}
lblPageCount.Text = this.PageCount.ToString();
this.lblMaxPage.Text = "共"+this.NMax.ToString()+"条记录";
this.txtCurrentPage.Text = this.PageCurrent.ToString();
winform增删改查简单案例 }
if (this.PageCurrent > this.PageCount)
{
this.PageCurrent = this.PageCount;
}
if (this.PageCount == 1)
{
this.PageCurrent = 1;
}
lblPageCount.Text = this.PageCount.ToString();
this.lblMaxPage.Text = "共"+this.NMax.ToString()+"条记录";
this.txtCurrentPage.Text = this.PageCurrent.ToString();
if (this.PageCurrent == 1)
{
this.btnPrev.Enabled = false;
this.btnFirst.Enabled = false;
}
else
{
btnPrev.Enabled = true;
btnFirst.Enabled = true;
}
if (this.PageCurrent == this.PageCount)
{
this.btnLast.Enabled = false;
this.btnNext.Enabled = false;
{
this.btnPrev.Enabled = false;
this.btnFirst.Enabled = false;
}
else
{
btnPrev.Enabled = true;
btnFirst.Enabled = true;
}
if (this.PageCurrent == this.PageCount)
{
this.btnLast.Enabled = false;
this.btnNext.Enabled = false;
}
else
{
btnLast.Enabled = true;
btnNext.Enabled = true;
}
if (this.NMax == 0)
{
btnNext.Enabled = false;
btnLast.Enabled = false;
btnFirst.Enabled = false;
btnPrev.Enabled = false;
}
}
else
{
btnLast.Enabled = true;
btnNext.Enabled = true;
}
if (this.NMax == 0)
{
btnNext.Enabled = false;
btnLast.Enabled = false;
btnFirst.Enabled = false;
btnPrev.Enabled = false;
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
PageCurrent = 1;
this.Bind();
}
private void btnPrev_Click(object sender, EventArgs e)
{
PageCurrent -= 1;
if (PageCurrent <= 0)
{
PageCurrent = 1;
}
this.Bind();
}
private void btnNext_Click(object sender, EventArgs e)
{
this.PageCurrent += 1;
if (PageCurrent > PageCount)
{
PageCurrent = PageCount;
}
this.Bind();
}
private void btnLast_Click(object sender, EventArgs e)
{
PageCurrent = PageCount;
private void btnNext_Click(object sender, EventArgs e)
{
this.PageCurrent += 1;
if (PageCurrent > PageCount)
{
PageCurrent = PageCount;
}
this.Bind();
}
private void btnLast_Click(object sender, EventArgs e)
{
PageCurrent = PageCount;
this.Bind();
}
private void btnGo_Click(object sender, EventArgs e)
{
if (this.txtCurrentPage.Text != null && txtCurrentPage.Text != "")
{
if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
{
this.Bind();
}
else
{
Common.MessageProcess.ShowError("输入数字格式错误!");
}
}
private void btnGo_Click(object sender, EventArgs e)
{
if (this.txtCurrentPage.Text != null && txtCurrentPage.Text != "")
{
if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
{
this.Bind();
}
else
{
Common.MessageProcess.ShowError("输入数字格式错误!");
}
}
}
}
/**//// <summary>
/// 自定义事件数据基类
/// </summary>
public class EventPagingArg : EventArgs
{
private int _intPageIndex;
public EventPagingArg(int PageIndex)
{
_intPageIndex = PageIndex;
}
}
}
}
}
/**//// <summary>
/// 自定义事件数据基类
/// </summary>
public class EventPagingArg : EventArgs
{
private int _intPageIndex;
public EventPagingArg(int PageIndex)
{
_intPageIndex = PageIndex;
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论