发现在winform中的DataGridView居然是没有分页的,本来准备去写个但是又没那么多时间去构思就在园子里面找了一个,我把修改以后的发上来已经添加了注释,原出处为:http://www.cnblogs.com/flood/archive/2005/05/13/154527.html;非常感谢原作者;
设计部分:页面
Code
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnGo = new System.Windows.Forms.Button();
this.lblPageNum = new System.Windows.Forms.Label();
this.txtBxNumber = new System.Windows.Forms.TextBox();
this.lnkLast = new System.Windows.Forms.LinkLabel();
this.lnkNext = new System.Windows.Forms.LinkLabel();
this.lnkPrev = new System.Windows.Forms.LinkLabel();
this.lnkFirst = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(263, 3);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(34, 23);
this.btnGo.TabIndex = 48;
this.btnGo.Text = "GO";
this.btnGo.UseVisualStyleBackColor = true;
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// lblPageNum
//
this.lblPageNum.AutoSize = true;
this.lblPageNum.Location = new System.Drawing.Point(322, 9);
this.lblPageNum.Name = "lblPageNum";
this.lblPageNum.Size = new System.Drawing.Size(35, 12);
this.lblPageNum.TabIndex = 47;
this.lblPageNum.Text = "1/1页";
//
// txtBxNumber
//
this.txtBxNumber.Location = new System.Drawing.Point(222, 5);
this.txtBxNumber.Name = "txtBxNumber";
this.txtBxNumber.Size = new System.Drawing.Size(24, 21);
this.txtBxNumber.TabIndex = 46;
//
// lnkLast
//
this.lnkLast.AutoSize = true;
this.lnkLast.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkLast.LinkColor = System.Drawing.Color.Black;
this.lnkLast.Location = new System.Drawing.Point(175, 9);
this.lnkLast.Name = "lnkLast";
this.lnkLast.Size = new System.Drawing.Size(41, 12);
this.lnkLast.TabIndex = 45;
this.lnkLast.TabStop = true;
this.lnkLast.Text = "最后页";
this.lnkLast.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkLast_LinkClicked);
//
// lnkNext
//
this.lnkNext.AutoSize = true;
this.lnkNext.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkNext.LinkColor = System.Drawing.Color.Black;
this.lnkNext.Location = new System.Drawing.Point(126, 9);
this.lnkNext.Name = "lnkNext";
this.lnkNext.Size = new System.Drawing.Size(41, 12);
this.lnkNext.TabIndex = 44;
this.lnkNext.TabStop = true;
this.lnkNext.Text = "下一页";
this.lnkNext.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkNext_LinkClicked);
//
// lnkPrev
//
this.lnkPrev.AutoSize = true;
this.lnkPrev.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkPrev.LinkColor = System.Drawing.Color.Black;
this.lnkPrev.Location = new System.Drawing.Point(77, 9);
this.lnkPrev.Name = "lnkPrev";
this.lnkPrev.Size = new System.Drawing.Size(41, 12);
this.lnkPrev.TabIndex = 43;
this.lnkPrev.TabStop = true;
this.lnkPrev.Text = "上一页";
this.lnkPrev.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkPrev_LinkClicked);
//
// lnkFirst
//
this.lnkFirst.AutoSize = true;
this.lnkFirst.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkFirst.LinkColor = System.Drawing.Color.Black;
this.lnkFirst.Location = new System.Drawing.Point(30, 9);
this.lnkFirst.Name = "lnkFirst";
this.lnkFirst.Size = new System.Drawing.Size(41, 12);
this.lnkFirst.TabIndex = 42;
this.lnkFirst.TabStop = true;
this.lnkFirst.Text = "第一页";
this.lnkFirst.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkFirst_LinkClicked);
//
// PaperControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(241)))), ((int)(((byte)(248)))), ((int)(((byte)(255)))));
this.Controls.Add(this.btnGo);
this.Controls.Add(this.lblPageNum);
this.Controls.Add(this.txtBxNumber);
this.Controls.Add(this.lnkLast);
this.Controls.Add(this.lnkNext);
this.Controls.Add(this.lnkPrev);
this.Controls.Add(this.lnkFirst);
this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(78)))), ((int)(((byte)(151)))));
this.Name = "PaperControl";
this.Size = new System.Drawing.Size(386, 29);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnGo;
private System.Windows.Forms.Label lblPageNum;
private System.Windows.Forms.TextBox txtBxNumber;
private System.Windows.Forms.LinkLabel lnkLast;
private System.Windows.Forms.LinkLabel lnkNext;
private System.Windows.Forms.LinkLabel lnkPrev;
private System.Windows.Forms.LinkLabel lnkFirst;
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnGo = new System.Windows.Forms.Button();
this.lblPageNum = new System.Windows.Forms.Label();
this.txtBxNumber = new System.Windows.Forms.TextBox();
this.lnkLast = new System.Windows.Forms.LinkLabel();
this.lnkNext = new System.Windows.Forms.LinkLabel();
this.lnkPrev = new System.Windows.Forms.LinkLabel();
this.lnkFirst = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(263, 3);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(34, 23);
this.btnGo.TabIndex = 48;
this.btnGo.Text = "GO";
this.btnGo.UseVisualStyleBackColor = true;
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// lblPageNum
//
this.lblPageNum.AutoSize = true;
this.lblPageNum.Location = new System.Drawing.Point(322, 9);
this.lblPageNum.Name = "lblPageNum";
this.lblPageNum.Size = new System.Drawing.Size(35, 12);
this.lblPageNum.TabIndex = 47;
this.lblPageNum.Text = "1/1页";
//
// txtBxNumber
//
this.txtBxNumber.Location = new System.Drawing.Point(222, 5);
this.txtBxNumber.Name = "txtBxNumber";
this.txtBxNumber.Size = new System.Drawing.Size(24, 21);
this.txtBxNumber.TabIndex = 46;
//
// lnkLast
//
this.lnkLast.AutoSize = true;
this.lnkLast.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkLast.LinkColor = System.Drawing.Color.Black;
this.lnkLast.Location = new System.Drawing.Point(175, 9);
this.lnkLast.Name = "lnkLast";
this.lnkLast.Size = new System.Drawing.Size(41, 12);
this.lnkLast.TabIndex = 45;
this.lnkLast.TabStop = true;
this.lnkLast.Text = "最后页";
this.lnkLast.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkLast_LinkClicked);
//
// lnkNext
//
this.lnkNext.AutoSize = true;
this.lnkNext.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkNext.LinkColor = System.Drawing.Color.Black;
this.lnkNext.Location = new System.Drawing.Point(126, 9);
this.lnkNext.Name = "lnkNext";
this.lnkNext.Size = new System.Drawing.Size(41, 12);
this.lnkNext.TabIndex = 44;
this.lnkNext.TabStop = true;
this.lnkNext.Text = "下一页";
this.lnkNext.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkNext_LinkClicked);
//
// lnkPrev
//
this.lnkPrev.AutoSize = true;
this.lnkPrev.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkPrev.LinkColor = System.Drawing.Color.Black;
this.lnkPrev.Location = new System.Drawing.Point(77, 9);
this.lnkPrev.Name = "lnkPrev";
this.lnkPrev.Size = new System.Drawing.Size(41, 12);
this.lnkPrev.TabIndex = 43;
this.lnkPrev.TabStop = true;
this.lnkPrev.Text = "上一页";
this.lnkPrev.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkPrev_LinkClicked);
//
// lnkFirst
//
this.lnkFirst.AutoSize = true;
this.lnkFirst.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lnkFirst.LinkColor = System.Drawing.Color.Black;
this.lnkFirst.Location = new System.Drawing.Point(30, 9);
this.lnkFirst.Name = "lnkFirst";
this.lnkFirst.Size = new System.Drawing.Size(41, 12);
this.lnkFirst.TabIndex = 42;
this.lnkFirst.TabStop = true;
this.lnkFirst.Text = "第一页";
this.lnkFirst.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkFirst_LinkClicked);
//
// PaperControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(241)))), ((int)(((byte)(248)))), ((int)(((byte)(255)))));
this.Controls.Add(this.btnGo);
this.Controls.Add(this.lblPageNum);
this.Controls.Add(this.txtBxNumber);
this.Controls.Add(this.lnkLast);
this.Controls.Add(this.lnkNext);
this.Controls.Add(this.lnkPrev);
this.Controls.Add(this.lnkFirst);
this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(78)))), ((int)(((byte)(151)))));
this.Name = "PaperControl";
this.Size = new System.Drawing.Size(386, 29);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnGo;
private System.Windows.Forms.Label lblPageNum;
private System.Windows.Forms.TextBox txtBxNumber;
private System.Windows.Forms.LinkLabel lnkLast;
private System.Windows.Forms.LinkLabel lnkNext;
private System.Windows.Forms.LinkLabel lnkPrev;
private System.Windows.Forms.LinkLabel lnkFirst;
cs代码:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace ASGExperiment.Log
{
public partial class PaperControl : UserControl
{
#region 设计部分
#region 参数及其他
private int displayCount;
private int perPage;
private int pageCount;
private int currentPage;
private int prevPage;
public event EventHandler currentPageChanged;
public int DisplayCount
{
get { return displayCount; }
}
public int PerPage
{
get { return perPage; }
}
public int PageCount
{
get { return pageCount; }
}
public int CurrentPage
{
get { return currentPage; }
}
#endregion
public PaperControl()
{
InitializeComponent();
displayCount = 0;
perPage = 0;
pageCount = 1;
currentPage = 1;
prevPage = 0;
}
#endregion
#region 核心部分
public void Init(int count, int perpage)
{
displayCount = Math.Max(count, 1);
perPage = Math.Min(perpage, displayCount);
pageCount = displayCount / perPage;
if (displayCount % perPage != 0)
pageCount++;
currentPage = 1;
DrawControl();
}
private void DrawControl()
{
lblPageNum.Text = currentPage.ToString() + "/" + pageCount.ToString();
if (prevPage != currentPage && currentPageChanged != null)
currentPageChanged(this, null);
prevPage = currentPage;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtBxNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (Char)8)
e.Handled = true;
}
/// <summary>
/// 页数选择限制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtBxNumber_TextChanged(object sender, EventArgs e)
{
if (txtBxNumber.Text.Length > 0 && int.Parse(txtBxNumber.Text) > pageCount)
{
txtBxNumber.Text = pageCount.ToString();
}
}
private void btnGo_Click(object sender, EventArgs e)
{
//忽略错误
//if (txtBxNumber.Text.Length == 0 || int.Parse(txtBxNumber.Text) > pageCount)
// return;
currentPage = int.Parse(txtBxNumber.Text);
DrawControl();
}
#endregion
private void lnkFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = 1;
DrawControl();
}
private void lnkPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = Math.Max(1, currentPage - 1);
DrawControl();
}
private void lnkNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = Math.Min(pageCount, currentPage + 1);
DrawControl();
}
private void lnkLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = pageCount;
DrawControl();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace ASGExperiment.Log
{
public partial class PaperControl : UserControl
{
#region 设计部分
#region 参数及其他
private int displayCount;
private int perPage;
private int pageCount;
private int currentPage;
private int prevPage;
public event EventHandler currentPageChanged;
public int DisplayCount
{
get { return displayCount; }
}
public int PerPage
{
get { return perPage; }
}
public int PageCount
{
get { return pageCount; }
}
public int CurrentPage
{
get { return currentPage; }
}
#endregion
public PaperControl()
{
InitializeComponent();
displayCount = 0;
perPage = 0;
pageCount = 1;
currentPage = 1;
prevPage = 0;
}
#endregion
#region 核心部分
public void Init(int count, int perpage)
{
displayCount = Math.Max(count, 1);
perPage = Math.Min(perpage, displayCount);
pageCount = displayCount / perPage;
if (displayCount % perPage != 0)
pageCount++;
currentPage = 1;
DrawControl();
}
private void DrawControl()
{
lblPageNum.Text = currentPage.ToString() + "/" + pageCount.ToString();
if (prevPage != currentPage && currentPageChanged != null)
currentPageChanged(this, null);
prevPage = currentPage;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtBxNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (Char)8)
e.Handled = true;
}
/// <summary>
/// 页数选择限制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtBxNumber_TextChanged(object sender, EventArgs e)
{
if (txtBxNumber.Text.Length > 0 && int.Parse(txtBxNumber.Text) > pageCount)
{
txtBxNumber.Text = pageCount.ToString();
}
}
private void btnGo_Click(object sender, EventArgs e)
{
//忽略错误
//if (txtBxNumber.Text.Length == 0 || int.Parse(txtBxNumber.Text) > pageCount)
// return;
currentPage = int.Parse(txtBxNumber.Text);
DrawControl();
}
#endregion
private void lnkFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = 1;
DrawControl();
}
private void lnkPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = Math.Max(1, currentPage - 1);
DrawControl();
}
private void lnkNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = Math.Min(pageCount, currentPage + 1);
DrawControl();
}
private void lnkLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
currentPage = pageCount;
DrawControl();
}
引用示例:
this.paperControl1.Init(count, perpage);
Init是设置总长度和每页多少条,他会自动调用currentPageChanged的,但是控件放到页面上以后也要绑定currentPageChanged的绑定页面列表的事件,所以绑定页面列表的方法写成事件,直接绑定给控件的currentPageChanged,而没必要在Load里面再放一个绑定事件了,那样容易引起死循环(这一点很重要)
如果还有不明白的可以跟帖提出来,我们一起探讨一下!