c#.net DataGridView Pager 数据绑定分页功能
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;
namespace Nbjjy
{
/// <summary>
///this.pager1.DataControl = this.dataGridView1;
///this.pager1.PageSize = 10;
///this.pager1.DataSource =DataTable;
///this.pager1.DataBind();
/// </summary>
public partial class Pager : UserControl
{
public Pager()
{
InitializeComponent();
}
/// <summary>
/// 首页
/// </summary>
private void BtnFirst_Click(object sender, EventArgs e)
{
if (CheckFillButton() == false)
{
return;
}
if (CurPage == 1)
{
MessageBox.Show("您已经在第一页了!", "提示");
return;
}
CurPage = 1;
recNo = 0;
LoadPage();
}
/// <summary>
/// 第一页
/// </summary>
private void BtnPrev_Click(object sender, EventArgs e)
{
if (CheckFillButton() == false)
{
return;
}
if (CurPage == PageCount)
{
recNo = PageSize * (CurPage - 2);
}
CurPage -= 1;
if (CurPage < 1)
{
MessageBox.Show("您已经在第一页了!", "提示");
CurPage = 1;
return;
}
else
{
recNo = PageSize * (CurPage - 1);
}
LoadPage();
}
/// <summary>
/// 下一页
/// </summary>
private void BtnNext_Click(object sender, EventArgs e)
{
if (CheckFillButton() == false)
{
return;
}
if (PageSize == 0)
{
MessageBox.Show("设置页数,按回车键进行数据填充!", "提示");
return;
}
CurPage += 1;
if (CurPage > PageCount)
{
CurPage = PageCount;
if (recNo == RecordCount)
{
MessageBox.Show("您已经在最后一页了!", "提示");
return;
}
}
LoadPage();
}
/// <summary>
/// 最后页
/// </summary>
private void BtnLast_Click(object sender, EventArgs e)
{
if (CheckFillButton() == false)
{
return;
}
if (recNo == RecordCount)
{
MessageBox.Show("您已经在最后一页了!", "提示");
return;
}
CurPage = PageCount;
recNo = PageSize * (CurPage - 1);
LoadPage();
}
public static int GetIntOrZero(string value)
{
try
{
return Convert.ToInt32(value.Trim());
}
catch
{
return 0;
}
}
private bool CheckFillButton()
{
if (PageSize == 0)
{
MessageBox.Show("设置页数,按回车键进行数据填充!", "提示");
return false;
}
else
{
return true;
}
}
/// <summary>
/// 绑定数据
/// </summary>
public void DataBind()
{
try
{
if (PageSize == 0 && PageSize == null)
{
PageSize = pageSize;
}
RecordCount = DataSource.Rows.Count;
if (PageSize == 0)
{
MessageBox.Show("页数不能为零,请输入大于零的数值!", "提示");
return;
}
PageCount = RecordCount / PageSize;
if ((RecordCount % PageSize) > 0)
{
PageCount += 1;
}
CurPage = 1;
recNo = 0;
LoadPage();
}
catch
{
throw;
}
}
public DataTable DataSource
{
get
{
return myDataTable;
}
set
{
myDataTable = value;
}
}
public DataGridView DataControl
{
get
{
return myDataGrid;
}
set
{
myDataGrid = value;
}
}
private void LoadPage()
{
int i;
int startRec;
int endRec;
DataTable dtTemp;
dtTemp = myDataTable.Clone();
if (CurPage == PageCount)
{
endRec = RecordCount;
}
else
{
endRec = PageSize * CurPage;
}
startRec = recNo;
for (i = startRec; i < endRec; i++)
{
dtTemp.ImportRow(myDataTable.Rows);
recNo += 1;
}
myDataGrid.DataSource = dtTemp;
this.LblTotalRecord.Text = string.Format("共 {0} 条记录,每页 {1} 条,共 {2} 页", RecordCount.ToString(), PageSize.ToString(), PageCount.ToString());
LblCurPage.Text = CurPage.ToString() + "/" + PageCount.ToString();
}
public int CurPage
{
get
{
if (curPage <= 0)
{
curPage = 1;
}
return curPage;
}
set
{
curPage = value;
}
}
public int RecordCount
{
get
{
return this.recordCount;
}
set
{
this.recordCount = value;
}
}
public int PageCount
{
get
{
return this.pageCount;
}
set
{
this.pageCount = value;
}
}
public int PageSize
{
get
{
if (pageSize <= 0)
{
pageSize = 15;
}
return pageSize;
}
set
{
pageSize = value;
}
}
private int recordCount;
private int pageCount;
private int pageSize;
private int curPage;
private int recNo;
DataGridView myDataGrid;
DataTable myDataTable;
}
}
//--------------------------/
namespace Nbjjy
{
partial class Pager
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.LblTotalRecord = new System.Windows.Forms.Label();
this.LblCurPage = new System.Windows.Forms.Label();
this.BtnLast = new System.Windows.Forms.Button();
this.BtnNext = new System.Windows.Forms.Button();
this.BtnPrev = new System.Windows.Forms.Button();
this.BtnFirst = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// LblTotalRecord
//
this.LblTotalRecord.Location = new System.Drawing.Point(1, 0);
this.LblTotalRecord.Name = "LblTotalRecord";
this.LblTotalRecord.Size = new System.Drawing.Size(231, 22);
this.LblTotalRecord.TabIndex = 13;
this.LblTotalRecord.Text = "共 0 条记录,每页 15 条,共 0 页";
this.LblTotalRecord.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// LblCurPage
//
this.LblCurPage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.LblCurPage.Location = new System.Drawing.Point(303, 0);
this.LblCurPage.Name = "LblCurPage";
this.LblCurPage.Size = new System.Drawing.Size(60, 22);
this.LblCurPage.TabIndex = 12;
this.LblCurPage.Text = "1/1";
this.LblCurPage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// BtnLast
//
this.BtnLast.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.BtnLast.Cursor = System.Windows.Forms.Cursors.Hand;
this.BtnLast.FlatAppearance.BorderSize = 0;
this.BtnLast.Location = new System.Drawing.Point(400, 0);
this.BtnLast.Margin = new System.Windows.Forms.Padding(5);
this.BtnLast.Name = "BtnLast";
this.BtnLast.Size = new System.Drawing.Size(25, 22);
this.BtnLast.TabIndex = 11;
this.BtnLast.Text = "|>";
this.BtnLast.UseVisualStyleBackColor = false;
this.BtnLast.Click += new System.EventHandler(this.BtnLast_Click);
//
// BtnNext
//
this.BtnNext.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.BtnNext.Cursor = System.Windows.Forms.Cursors.Hand;
this.BtnNext.FlatAppearance.BorderSize = 0;
this.BtnNext.Location = new System.Drawing.Point(370, 0);
this.BtnNext.Margin = new System.Windows.Forms.Padding(5);
this.BtnNext.Name = "BtnNext";
this.BtnNext.Size = new System.Drawing.Size(25, 22);
this.BtnNext.TabIndex = 10;
this.BtnNext.Text = ">";
this.BtnNext.UseVisualStyleBackColor = false;
this.BtnNext.Click += new System.EventHandler(this.BtnNext_Click);
//
// BtnPrev
//
this.BtnPrev.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.BtnPrev.Cursor = System.Windows.Forms.Cursors.Hand;
this.BtnPrev.FlatAppearance.BorderSize = 0;
this.BtnPrev.Location = new System.Drawing.Point(273, 0);
this.BtnPrev.Margin = new System.Windows.Forms.Padding(5);
this.BtnPrev.Name = "BtnPrev";
this.BtnPrev.Size = new System.Drawing.Size(25, 22);
this.BtnPrev.TabIndex = 9;
this.BtnPrev.Text = "<";
this.BtnPrev.UseVisualStyleBackColor = false;
this.BtnPrev.Click += new System.EventHandler(this.BtnPrev_Click);
//
// BtnFirst
//
this.BtnFirst.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.BtnFirst.Cursor = System.Windows.Forms.Cursors.Hand;
this.BtnFirst.FlatAppearance.BorderSize = 0;
this.BtnFirst.Location = new System.Drawing.Point(240, 0);
this.BtnFirst.Margin = new System.Windows.Forms.Padding(5);
this.BtnFirst.Name = "BtnFirst";
this.BtnFirst.Size = new System.Drawing.Size(25, 22);
this.BtnFirst.TabIndex = 8;
this.BtnFirst.Text = "<|";
this.BtnFirst.UseVisualStyleBackColor = true;
this.BtnFirst.Click += new System.EventHandler(this.BtnFirst_Click);
//
// Pager
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Control;
this.Controls.Add(this.LblTotalRecord);
this.Controls.Add(this.LblCurPage);
this.Controls.Add(this.BtnLast);
this.Controls.Add(this.BtnNext);
this.Controls.Add(this.BtnPrev);
this.Controls.Add(this.BtnFirst);
this.Name = "Pager";
this.Size = new System.Drawing.Size(430, 22);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label LblTotalRecord;
private System.Windows.Forms.Label LblCurPage;
private System.Windows.Forms.Button BtnLast;
private System.Windows.Forms.Button BtnNext;
private System.Windows.Forms.Button BtnPrev;
private System.Windows.Forms.Button BtnFirst;
}
}
页:
[1]