📄 datagridpager.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace GzglClient.Views
{
/// <summary>
/// DataGridPager 的摘要说明。
/// </summary>
public class DataGridPager : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox txtPageSize;
private System.Windows.Forms.TextBox txtDisplayPageNo;
private System.Windows.Forms.LinkLabel btnLastPage;
private System.Windows.Forms.LinkLabel btnNextPage;
private System.Windows.Forms.LinkLabel btnPreviousPage;
private System.Windows.Forms.LinkLabel btnFirstPage;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
int PageCount;
int maxRec;
int pageSize;
int currentPage;
int recNo;
DataGrid myDataGrid;
private System.Windows.Forms.ErrorProvider errorProvider1;
DataTable myDataTable;
public DataGridPager()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtPageSize = new System.Windows.Forms.TextBox();
this.txtDisplayPageNo = new System.Windows.Forms.TextBox();
this.btnLastPage = new System.Windows.Forms.LinkLabel();
this.btnNextPage = new System.Windows.Forms.LinkLabel();
this.btnPreviousPage = new System.Windows.Forms.LinkLabel();
this.btnFirstPage = new System.Windows.Forms.LinkLabel();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
this.SuspendLayout();
//
// txtPageSize
//
this.txtPageSize.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtPageSize.Location = new System.Drawing.Point(414, 16);
this.txtPageSize.Name = "txtPageSize";
this.txtPageSize.TabIndex = 17;
this.txtPageSize.Text = "50";
this.txtPageSize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtPageSize_KeyPress);
//
// txtDisplayPageNo
//
this.txtDisplayPageNo.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtDisplayPageNo.Location = new System.Drawing.Point(8, 16);
this.txtDisplayPageNo.Name = "txtDisplayPageNo";
this.txtDisplayPageNo.Size = new System.Drawing.Size(120, 14);
this.txtDisplayPageNo.TabIndex = 16;
this.txtDisplayPageNo.Text = "";
//
// btnLastPage
//
this.btnLastPage.Location = new System.Drawing.Point(342, 16);
this.btnLastPage.Name = "btnLastPage";
this.btnLastPage.Size = new System.Drawing.Size(56, 16);
this.btnLastPage.TabIndex = 15;
this.btnLastPage.TabStop = true;
this.btnLastPage.Text = "最后一页";
this.btnLastPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.btnLastPage_LinkClicked);
//
// btnNextPage
//
this.btnNextPage.Location = new System.Drawing.Point(278, 16);
this.btnNextPage.Name = "btnNextPage";
this.btnNextPage.Size = new System.Drawing.Size(56, 16);
this.btnNextPage.TabIndex = 14;
this.btnNextPage.TabStop = true;
this.btnNextPage.Text = "下一页";
this.btnNextPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.btnNextPage_LinkClicked);
//
// btnPreviousPage
//
this.btnPreviousPage.Location = new System.Drawing.Point(206, 16);
this.btnPreviousPage.Name = "btnPreviousPage";
this.btnPreviousPage.Size = new System.Drawing.Size(56, 16);
this.btnPreviousPage.TabIndex = 13;
this.btnPreviousPage.TabStop = true;
this.btnPreviousPage.Text = "前一页";
this.btnPreviousPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.btnPreviousPage_LinkClicked);
//
// btnFirstPage
//
this.btnFirstPage.Location = new System.Drawing.Point(142, 16);
this.btnFirstPage.Name = "btnFirstPage";
this.btnFirstPage.Size = new System.Drawing.Size(56, 16);
this.btnFirstPage.TabIndex = 12;
this.btnFirstPage.TabStop = true;
this.btnFirstPage.Text = "第一页";
this.btnFirstPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.btnFirstPage_LinkClicked);
//
// errorProvider1
//
this.errorProvider1.ContainerControl = this;
//
// DataGridPager
//
this.Controls.Add(this.txtPageSize);
this.Controls.Add(this.txtDisplayPageNo);
this.Controls.Add(this.btnLastPage);
this.Controls.Add(this.btnNextPage);
this.Controls.Add(this.btnPreviousPage);
this.Controls.Add(this.btnFirstPage);
this.Name = "DataGridPager";
this.Size = new System.Drawing.Size(544, 32);
this.ResumeLayout(false);
}
#endregion
public DataTable DataSource
{
get
{
return myDataTable;
}
set
{
myDataTable=value;
}
}
public DataGrid DataGridName
{
get
{
return myDataGrid;
}
set
{
myDataGrid=value;
}
}
private void LoadPage()
{
int i;
int startRec;
int endRec;
DataTable dtTemp;
//Clone the source table to create a temporary table.
dtTemp = myDataTable.Clone();
if (currentPage == PageCount)
{
endRec = maxRec;
}
else
{
endRec = pageSize * currentPage;
}
startRec = recNo;
//Copy rows from the source table to fill the temporary table.
for (i = startRec; i < endRec; i++)
{
dtTemp.ImportRow(myDataTable.Rows[i]);
recNo += 1;
}
myDataGrid.DataSource = dtTemp;
DisplayPageInfo();
}
private void DisplayPageInfo()
{
txtDisplayPageNo.Text = "Page " + currentPage.ToString() + "/ " + PageCount.ToString();
}
private bool CheckFillButton()
{
// Check if the user clicks the "Fill Grid" button.
if (pageSize == 0)
{
MessageBox.Show("设置页数,按回车键进行数据填充!","提示");
return false;
}
else
{
return true;
}
}
public void FillData()
{
try
{
pageSize = Convert.ToInt32(txtPageSize.Text);
maxRec = DataSource.Rows.Count;
if (pageSize==0)
{
MessageBox.Show("页数不能为零,请输入大于零的数值!","提示");
return;
}
PageCount = maxRec / pageSize;
//Adjust the page number if the last page contains a partial page.
if ((maxRec % pageSize) > 0)
{
PageCount += 1;
}
// Initial seeings
currentPage = 1;
recNo = 0;
// Display the content of the current page.
LoadPage();
}
catch
{
throw;
}
}
private void btnFirstPage_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
if (CheckFillButton() == false)
{
return;
}
//Check if you are already at the first page.
if (currentPage == 1)
{
MessageBox.Show("您已经在第一页了!","提示");
return;
}
currentPage = 1;
recNo = 0;
LoadPage();
}
private void btnPreviousPage_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
if (CheckFillButton() == false)
{
return;
}
if (currentPage == PageCount)
{
recNo = pageSize * (currentPage - 2);
}
currentPage -= 1;
//Check if you are already at the first page.
if (currentPage < 1)
{
MessageBox.Show("您已经在第一页了!","提示");
currentPage = 1;
return;
}
else
{
recNo = pageSize * (currentPage - 1);
}
LoadPage();
}
private void btnNextPage_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
//If the user did not click the "Fill Grid" button, then return.
if (CheckFillButton() == false)
{
return;
}
//Check if the user clicks the "Fill Grid" button.
if (pageSize == 0)
{
MessageBox.Show("设置页数,按回车键进行数据填充!","提示");
return;
}
currentPage += 1;
if (currentPage > PageCount)
{
currentPage = PageCount;
//Check if you are already at the last page.
if (recNo == maxRec)
{
MessageBox.Show("您已经在最后一页了!","提示");
return;
}
}
LoadPage();
}
private void btnLastPage_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
if (CheckFillButton() == false)
{
return;
}
//Check if you are already at the last page.
if (recNo == maxRec)
{
MessageBox.Show("您已经在最后一页了!","提示");
return;
}
currentPage = PageCount;
recNo = pageSize * (currentPage - 1);
LoadPage();
}
private void txtPageSize_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar==13)
{
if (IsNumeric(txtPageSize.Text))
{
FillData();
errorProvider1.SetError(txtPageSize, "");
}
else
{
errorProvider1.SetError(txtPageSize, "不是个整数!");
}
}
}
static bool IsNumeric(string str)
{
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
return reg1.IsMatch(str);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -