本例子使用自定义控件方法实现,数据库使用的是SQL Server,实现过程如下:
1、新建一个自定义控件,命名为:PageControl。
2、PageControl代码如下:
public partial class PageControl : UserControl { //委托及事件 public delegate void BindPage(int pageSize, int pageIndex, out int totalCount); public event BindPage BindPageEvent; //属性 public int PageSize { get; set; } = 1; //每页显示记录数 public int PageIndex { get; set; } //页序号 public int TotalCount { get; set; } //总记录数 public int PageCount { get; set; } //总页数 public PageControl() { InitializeComponent(); //取消下划线 linkFirst.LinkBehavior = LinkBehavior.NeverUnderline; linkPrev.LinkBehavior = LinkBehavior.NeverUnderline; linkNext.LinkBehavior = LinkBehavior.NeverUnderline; linkLast.LinkBehavior = LinkBehavior.NeverUnderline; linkGo.LinkBehavior = LinkBehavior.NeverUnderline; } /// <summary> /// 设置页 /// </summary> public void SetPage() { //总记录数 int totalCount = 0; BindPageEvent(PageSize, PageIndex + 1, out totalCount); TotalCount = totalCount; //总页数 if (TotalCount % PageSize == 0) PageCount = TotalCount / PageSize; else PageCount = TotalCount / PageSize + 1; //当前页及总页数 txtCurrentPage.Text = (PageIndex + 1).ToString(); lblTotalPage.Text = "共 " + PageCount.ToString() + " 页"; } /// <summary> /// 首页 /// </summary> /// <param></param> /// <param></param> private void linkFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex = 0; SetPage(); } } /// <summary> /// 上一页 /// </summary> /// <param></param> /// <param></param> private void linkPrve_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex--; if (PageIndex < 0) { PageIndex = 0; } SetPage(); } } /// <summary> /// 下一页 /// </summary> /// <param></param> /// <param></param> private void linkNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex++; if (PageIndex > PageCount - 1) { PageIndex = PageCount - 1; } SetPage(); } } /// <summary> /// 末页 /// </summary> /// <param></param> /// <param></param> private void linkLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex = PageCount - 1; SetPage(); } } /// <summary> /// 只能按0-9、Delete、Enter、Backspace键 /// </summary> /// <param></param> /// <param></param> private void txtSetPage_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8 || e.KeyChar == 13 || e.KeyChar == 127) { e.Handled = false; if (e.KeyChar == 13) { Go(); } } else { e.Handled = true; } } /// <summary> /// 指定页 /// </summary> /// <param></param> /// <param></param> private void linkGo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { Go(); } } private void Go() { if (string.IsNullOrEmpty(txtCurrentPage.Text)) { MessageBox.Show("指定页不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCurrentPage.Focus(); return; } if (int.Parse(txtCurrentPage.Text) > PageCount) { MessageBox.Show("指定页已超过总页数。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCurrentPage.Focus(); return; } PageIndex = int.Parse(txtCurrentPage.Text) - 1; SetPage(); } /// <summary> /// linkFirst鼠标移过颜色 /// </summary> /// <param></param> /// <param></param> private void linkFirst_MouseMove(object sender, MouseEventArgs e) { linkFirst.LinkColor = Color.Red; } /// <summary> /// linkFirst鼠标离开颜色 /// </summary> /// <param></param> /// <param></param> private void linkFirst_MouseLeave(object sender, EventArgs e) { linkFirst.LinkColor = Color.Black; } /// <summary> /// linkPrev鼠标移过颜色 /// </summary> /// <param></param> /// <param></param> private void linkPrev_MouseMove(object sender, MouseEventArgs e) { linkPrev.LinkColor = Color.Red; } /// <summary> /// linkPrev鼠标离开颜色 /// </summary> /// <param></param> /// <param></param> private void linkPrev_MouseLeave(object sender, EventArgs e) { linkPrev.LinkColor = Color.Black; } /// <summary> /// linkNext鼠标移过颜色 /// </summary> /// <param></param> /// <param></param> private void linkNext_MouseMove(object sender, MouseEventArgs e) { linkNext.LinkColor = Color.Red; } /// <summary> /// linkNext鼠标离开颜色 /// </summary> /// <param></param> /// <param></param> private void linkNext_MouseLeave(object sender, EventArgs e) { linkNext.LinkColor = Color.Black; } /// <summary> /// linkLast鼠标移过颜色 /// </summary> /// <param></param> /// <param></param> private void linkLast_MouseMove(object sender, MouseEventArgs e) { linkLast.LinkColor = Color.Red; } /// <summary> /// linkLast鼠标离开颜色 /// </summary> /// <param></param> /// <param></param> private void linkLast_MouseLeave(object sender, EventArgs e) { linkLast.LinkColor = Color.Black; } /// <summary> /// linkGo鼠标移过颜色 /// </summary> /// <param></param> /// <param></param> private void linkGo_MouseMove(object sender, MouseEventArgs e) { linkGo.LinkColor = Color.Red; } /// <summary> /// linkGo鼠标离开颜色 /// </summary> /// <param></param> /// <param></param> private void linkGo_MouseLeave(object sender, EventArgs e) { linkGo.LinkColor = Color.Black; } }
3、SQL Server创建存储过程PageTest: