由于本人写作能力很差,所以这里就只写代码了......哈哈哈
下面是分页用户控件的所有代码
public partial class PageNumControl : UserControl { public PageNumControl() { InitializeComponent(); } #region 分页字段和属性 private int pageIndex = 1; /// <summary> /// 当前页码 /// </summary> [Description("当前页码")] public virtual int PageIndex { get { return pageIndex; } set { pageIndex = value; } } private int pageSize = 15; /// <summary> /// 每页显示记录数 /// </summary> [Description("每页显示记录数")] public virtual int PageSize { get { return pageSize; } set { pageSize = value; } } public int EndIndex { get { return this.PageIndex * this.PageSize; } } public int Startindex { get { return (this.PageIndex - 1) * this.PageSize + 1; } } /// <summary> /// 页码偏移量 /// </summary> [Description("页码偏移量")] public int PageOffset { get; set; } /// <summary> /// 显示页码条数 /// </summary> [Description("显示页码条数")] public int PageBarNum { get; set; } = 10; /// <summary> /// 总记录数 /// </summary> private int recordCount = 0; /// <summary> /// 除当前页码其他页码字体 /// </summary> [Description("除当前页码其他页码字体")] public Font NumFont { get; set; } = new System.Drawing.Font("宋体", 9F); /// <summary> /// 当前页码字体 /// </summary> [Description("当前页码字体")] public Font CurrentNumFont { get; set; } = new System.Drawing.Font("宋体", 9F); /// <summary> /// 除当前页码其他页码颜色 /// </summary> [Description("除当前页码其他页码颜色")] public Color NumColor { get; set; } = Color.Black; /// <summary> /// 当前页码颜色 /// </summary> [Description("当前页码颜色")] public Color CurrentNumColor { get; set; } = Color.Red; /// <summary> /// 尾部显示内容 /// </summary> [Description("尾部显示内容")] public string LastShowText { get; set; } /// <summary> /// 总记录数 /// </summary> [Description("总记录数")] public virtual int RecordCount { get { return recordCount; } set { recordCount = value; } } private int pageCount = 0; /// <summary> /// 总页数 /// </summary> [Description("总页数")] public int PageCount { get { if (pageSize != 0) { pageCount = GetPageCount(); } return pageCount; } } /// <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; } return pageCount; } /// <summary> /// 每个页码之间的距离长度 /// </summary> [Description("页码间隔长度")] public int fwd { get; set; } = 10; /// <summary> /// 第一个模块距离左边的距离 /// </summary> [Description("第一个模块间隔长度")] public int FirstXLocation { get; set; } = 10; /// <summary> /// 页码y轴距离 /// </summary> [Description("页码y轴距离")] public int YLocation { get; set; } = 28; /// <summary> /// 页码宽度 /// </summary> [Description("页码宽度")] public int PageWidth { get; set; } = 179; /// <summary> /// 页码高度 /// </summary> [Description("页码高度")] public int PageHeight { get; set; } = 12; /// <summary> /// 首页图标 /// </summary> [Description("首页图标")] public string FirstPageTitle { get; set; } = "首页"; /// <summary> /// 下一页图标 /// </summary> [Description("下一页图标")] public string NextPageTitle { get; set; } = "下一页"; /// <summary> /// 上一页图标 /// </summary> [Description("上一页图标")] public string PrevPageTitle { get; set; } = "上一页"; /// <summary> /// 尾页图标 /// </summary> [Description("尾页图标")] public string LastPageTitle { get; set; } = "尾页"; /// <summary> /// 跳转页码文本框宽度 /// </summary> [Description("跳转页码文本框宽度")] public int TextBoxPageWidth { get; set; } = 30; /// <summary> /// 跳转页码文本框高度 /// </summary> [Description("跳转页码文本框高度")] public int TextBoxPageHeight { get; set; } = 17; /// <summary> /// 文本框页码字体 /// </summary> [Description("文本框页码字体")] public Font TextBoxPageFont { get; set; } = new System.Drawing.Font("宋体", 9F); /// <summary> /// 文本框页码颜色 /// </summary> [Description("文本框页码颜色")] public Color TextBoxPageColor { get; set; } = Color.Black; #endregion public event EventHandler OnPageChanged; /// <summary> /// 只有页码、首页、尾页、上一页、下一页 /// </summary> [Description("分页模板6")] public bool PageModel6 { get; set; } = false; /// <summary> /// 只有页码、首页、尾页、上一页、下一页 /// </summary> [Description("分页模板100")] public bool PageModel100 { get; set; } = false; /// <summary> /// 当前数据只有一页时设置的显示内容,不设置则显示默认内容 /// </summary> [Description("设置只有一页时展示的内容")] public string FirstPageMessage { get; set; } = ""; /// <summary> /// 添加动态点击页码控件 /// </summary> /// <param name="str">展示的内容</param> /// <param name="i">控件索引</param> /// <param name="fx">初始控件x轴距离</param> /// <param name="label1">控件</param> /// <param name="tag">临时页码数据</param> public void AddControl(string str, int i, int fx, ref Label label1, string tag = "", bool isEnable = true) { label1.AutoSize = true; label1.Location = new System.Drawing.Point(fx, YLocation); label1.Name = "PageLable" + i; label1.Size = new System.Drawing.Size(PageWidth, PageHeight); label1.TabIndex = i; label1.Text = str; label1.Tag = tag; label1.Enabled = isEnable; if (str == this.PageIndex.ToString()) { label1.Font = CurrentNumFont; label1.ForeColor = CurrentNumColor; } else { label1.Font = NumFont; label1.ForeColor = NumColor; label1.Cursor = Cursors.Hand; label1.Click += new EventHandler(PageNumControl1_OnPageChanged); } this.panel1.Controls.Add(label1); } /// <summary> /// 添加动态展示控件 /// </summary> /// <param name="str"></param> /// <param name="i"></param> /// <param name="fx"></param> /// <param name="label1"></param> /// <param name="tag"></param> public void AddShowControl(string str, int i, int fx, ref Label label1, string tag = "") { label1.AutoSize = true; label1.Location = new System.Drawing.Point(fx, YLocation); label1.Name = "PageLable" + i; label1.Size = new System.Drawing.Size(PageWidth, PageHeight); label1.TabIndex = i; label1.Text = str; label1.Tag = tag; label1.Font = NumFont; label1.ForeColor = NumColor; this.panel1.Controls.Add(label1); } public void AddTextControl(string str, int i, int fx, ref TextBox txtBox, string tag = "") { txtBox.Location = new System.Drawing.Point(fx, YLocation); txtBox.Multiline = true; txtBox.Name = "txtBox" + i; txtBox.Text = str; txtBox.Size = new System.Drawing.Size(TextBoxPageWidth, TextBoxPageHeight); txtBox.TabIndex = i; txtBox.Tag = tag; txtBox.Font = TextBoxPageFont; txtBox.TextAlign = HorizontalAlignment.Center; txtBox.ForeColor = TextBoxPageColor; this.panel1.Controls.Add(txtBox); } /// <summary> /// 分页模板6 /// </summary> public void GetPageModel6() { int totalCount = this.RecordCount; if (this.PageIndex <= 0) { this.PageIndex = 1; } int start = (this.PageIndex - 5 + this.PageOffset); if (start < 1) { start = 1; } int end = (start + this.PageBarNum - 1); if (end > this.PageCount) { start += this.PageCount - end; if (start <= 0) { start = 1; } end = this.PageCount; } if (this.PageCount > 1) { StringBuilder outPut = new StringBuilder(); if (this.EndIndex > totalCount) { outPut.AppendFormat("第{0}-{1}条,", this.Startindex, totalCount); } else { outPut.AppendFormat("第{0}-{1}条,", this.Startindex, this.EndIndex); } outPut.AppendFormat("每页{1}条,共{0}条", totalCount, this.PageSize); Label lb1 = new Label(); AddShowControl(outPut.ToString(), 1000000001, FirstXLocation, ref lb1, outPut.ToString()); Label lb3 = new Label(); if (this.PageIndex > 1) { Label lb2 = new Label(); AddControl(FirstPageTitle, 1000000002, lb1.Width + lb1.Location.X + fwd, ref lb2, "首页"); AddControl(PrevPageTitle, 1000000003, lb2.Width + lb2.Location.X + fwd, ref lb3, "上一页"); } else { lb3 = lb1; } Label lb5 = new Label(); int b = 0; for (int i = start; i <= end; i++) { b++; if (b > 1) { lb3 = lb5; lb5 = new Label(); } AddControl(i.ToString(), i + 1000, lb3.Width + lb3.Location.X + fwd, ref lb5, i.ToString()); } Label lb6 = new Label(); if (this.PageIndex != this.pageCount) { Label lb4 = new Label(); AddControl(NextPageTitle, 1000000004, lb5.Width + lb5.Location.X + fwd, ref lb4, "下一页"); AddControl(LastPageTitle, 1000000005, lb4.Width + lb4.Location.X + fwd, ref lb6, "尾页"); } else { lb6 = lb5; } Label lb7 = new Label(); if (!string.IsNullOrEmpty(LastShowText)) { AddShowControl(LastShowText, 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, LastShowText); } else { AddShowControl("第" + this.PageIndex + "页/共" + this.PageCount + "页", 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, "第" + this.PageIndex + "页/共" + this.PageCount + "页"); } TextBox tx = new TextBox(); AddTextControl(this.PageIndex.ToString(), 200, lb7.Width + lb7.Location.X + fwd, ref tx, ""); Label lb8 = new Label(); AddControl("跳转", 1000000009, tx.Width + tx.Location.X + fwd, ref lb8, "跳转"); } else { Label lb = new Label(); StringBuilder outPut = new StringBuilder(); if (string.IsNullOrEmpty(this.FirstPageMessage)) { outPut.AppendFormat("第{0}-{1}条,", this.Startindex, this.RecordCount); outPut.AppendFormat("每页{0}条,共{1}条", this.PageSize, this.RecordCount); } else { outPut.AppendFormat(this.FirstPageMessage); } AddShowControl(outPut.ToString(), 1002, fwd, ref lb, outPut.ToString()); } } /// <summary> /// 分页模板100 /// </summary> public void GetPageModel100() { int totalCount = this.RecordCount; if (this.PageIndex <= 0) { this.PageIndex = 1; } int start = (this.PageIndex - 5 + this.PageOffset); if (start < 1) { start = 1; } int end = (start + this.PageBarNum - 1); if (end > this.PageCount) { start += this.PageCount - end; if (start <= 0) { start = 1; } end = this.PageCount; } StringBuilder outPut = new StringBuilder(); if (this.EndIndex > totalCount) { outPut.AppendFormat("第{0}-{1}条,", this.Startindex, totalCount); } else { outPut.AppendFormat("第{0}-{1}条,", this.Startindex, this.EndIndex); } outPut.AppendFormat("每页{1}条,共{0}条", totalCount, this.PageSize); Label lb1 = new Label(); AddShowControl(outPut.ToString(), 1000000001, FirstXLocation, ref lb1, outPut.ToString()); Label lb3 = new Label(); bool isenable = true; if (this.PageIndex <= 1) { isenable = false; } Label lb2 = new Label(); AddControl(FirstPageTitle, 1000000002, lb1.Width + lb1.Location.X + fwd, ref lb2, "首页", isenable); AddControl(PrevPageTitle, 1000000003, lb2.Width + lb2.Location.X + fwd, ref lb3, "上一页", isenable); Label lb6 = new Label(); isenable = true; if (this.PageIndex == this.pageCount) { isenable = false; } Label lb4 = new Label(); AddControl(NextPageTitle, 1000000004, lb3.Width + lb3.Location.X + fwd, ref lb4, "下一页", isenable); AddControl(LastPageTitle, 1000000005, lb4.Width + lb4.Location.X + fwd, ref lb6, "尾页", isenable); Label lb7 = new Label(); if (!string.IsNullOrEmpty(LastShowText)) { AddShowControl(LastShowText, 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, LastShowText); } else { AddShowControl("第" + this.PageIndex + "页/共" + this.PageCount + "页", 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, "第" + this.PageIndex + "页/共" + this.PageCount + "页"); } TextBox tx = new TextBox(); AddTextControl(this.PageIndex.ToString(), 200, lb7.Width + lb7.Location.X + fwd, ref tx, ""); Label lb8 = new Label(); AddControl("跳转", 1000000009, tx.Width + tx.Location.X + fwd, ref lb8, "跳转"); } public void PageNumControl_Load(object sender, EventArgs e) { this.panel1.Controls.Clear(); this.PageIndex = 1; if (OnPageChanged != null) { OnPageChanged(this, null);//当前分页数字改变时,触发委托事件 } if (this.PageModel6) { GetPageModel6(); } else if (this.PageModel100) { GetPageModel100(); } } public void PageNumControl1_OnPageChanged(object sender, EventArgs e) { SetPageText(); this.panel1.Controls.Clear(); Label btn = sender as Label; if (btn.Tag.ToString().Trim() == "首页") { this.PageIndex = 1; } else if (btn.Tag.ToString().Trim() == "上一页") { this.PageIndex -= 1; } else if (btn.Tag.ToString().Trim() == "下一页") { this.PageIndex += 1; } else if (btn.Tag.ToString().Trim() == "尾页") { this.PageIndex = this.pageCount; } else if (btn.Tag.ToString().Trim() == "跳转") { // SetPageText(); } else { this.PageIndex = Convert.ToInt32(btn.Text); } if (OnPageChanged != null) { OnPageChanged(this, null);//当前分页数字改变时,触发委托事件 } if (this.PageModel6) { GetPageModel6(); } else if (this.PageModel100) { GetPageModel100(); } } public void SetPageText() { foreach (Control c in this.panel1.Controls) { if (c is TextBox) { TextBox tbox = (TextBox)c; if (tbox.Name == "txtBox200") { int pagenum = 1; int.TryParse(tbox.Text, out pagenum); if (pagenum == 0) { pagenum = 1; } this.PageIndex = pagenum; } } } } /// <summary> /// 更像列表数据 /// </summary> public void UpdateShowData() { if (OnPageChanged != null) { OnPageChanged(this, null);//当前分页数字改变时,触发委托事件 } PageNumControl_Load(null, null); }
然后下面是该用户控件使用方式,由于页面的页码条和按钮都是动态生成的,所以如果有需要特别设置的样式,可以在代码中定义对应的属性,在引用的时候进行设置
public FrmHome() { InitializeComponent(); pageNumControl1.OnPageChanged += new EventHandler(pageNumControl1_OnPageChanged); } public void BindData() { PageModelTool pl= ClockBll.GetTable(pageNumControl1.PageSize, pageNumControl1.PageIndex);//获取数据列表(该方法自己根据需要封装即可),将获取的table数据和总数等分页数据,封装到PageModelTool类中(自己定义封装属性,想要啥封装啥) gvClockList.AutoGenerateColumns = false; gvClockList.DataSource = pl.table; pageNumControl1.RecordCount = pl.Number;//数据总数 } private void pageNumControl1_OnPageChanged(object sender, EventArgs e) { BindData(); }
说明:FrmHome是窗体界面,gvClockList是DataGridView控件,pageNumControl1是用户分页控件
下面是分页用户控件设置的属性,可以直接复制到设计窗体后台代码中
this.pageNumControl1.CurrentNumColor = System.Drawing.Color.Red; this.pageNumControl1.CurrentNumFont = new System.Drawing.Font("宋体", 12F); this.pageNumControl1.Dock = System.Windows.Forms.DockStyle.Bottom; this.pageNumControl1.FirstPageMessage = ""; this.pageNumControl1.FirstPageTitle = "首页"; this.pageNumControl1.FirstXLocation = 10; this.pageNumControl1.ForeColor = System.Drawing.Color.Red; this.pageNumControl1.fwd = 10; this.pageNumControl1.LastPageTitle = "尾页"; this.pageNumControl1.LastShowText = null; this.pageNumControl1.Location = new System.Drawing.Point(0, 0); this.pageNumControl1.Margin = new System.Windows.Forms.Padding(5); this.pageNumControl1.Name = "pageNumControl1"; this.pageNumControl1.NextPageTitle = "下一页"; this.pageNumControl1.NumColor = System.Drawing.Color.Black; this.pageNumControl1.NumFont = new System.Drawing.Font("宋体", 12F); this.pageNumControl1.PageBarNum = 10; this.pageNumControl1.PageHeight = 12;this.pageNumControl1.PageModel100 = false;this.pageNumControl1.PageModel6 = true;// 这个属性表示你使用该分页模板来显示分页页码条 this.pageNumControl1.PageOffset = 0; this.pageNumControl1.PageSize = 24; this.pageNumControl1.PageWidth = 179; this.pageNumControl1.PrevPageTitle = "上一页"; this.pageNumControl1.RecordCount = 0; this.pageNumControl1.Size = new System.Drawing.Size(1130, 53); this.pageNumControl1.TabIndex = 0; this.pageNumControl1.TextBoxPageColor = System.Drawing.Color.Black; this.pageNumControl1.TextBoxPageFont = new System.Drawing.Font("宋体", 9F); this.pageNumControl1.TextBoxPageHeight = 17; this.pageNumControl1.TextBoxPageWidth = 48; this.pageNumControl1.YLocation = 20; this.pageNumControl1.OnPageChanged += new System.EventHandler(this.pageNumControl1_OnPageChanged);
下面就是最终的效果啦...
有需要的朋友希望可以帮到你,也可以在这个基础上封装自己需要的分页样式,我这边就只是封装了一个自己用的基本样式了