DataGridView实现分页

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;
using System.Collections; namespace XjxMobilenursing
{
public partial class PagerCtrl : UserControl
{
public PagerCtrl()
{
InitializeComponent();
}
[Browsable(true)]
[Category("页面记录数")]
[Description("显示页面记录数")]
[DefaultValue(typeof(int), "")]
public int PageSize{ get;set; }
public Hashtable condition { get; set; }//查询条件
private int totalCount = ;//总记录数
public int TotalCount
{
get { return totalCount; }
set
{
totalCount = value;
if (totalCount <= ) totalCount = ;
if (totalCount <= PageSize)
{
lkbNextPage.Enabled = false;
}
else
{
lkbNextPage.Enabled = true;
}
lkbPrePage.Enabled = false;
lblPageSize.Text = PageSize.ToString();
lblTotalCount.Text = totalCount.ToString();
lblPageNum.Text = PageNum.ToString();
}
}
public int BeginPage//起始记录
{
get
{
return (currentPage - ) * PageSize + ;
}
}
public int EndPage { //结束记录
get {
if ((CurrentPage < PageNum) || TotalCount % PageSize == )
{
return BeginPage + PageSize - ;
}
return BeginPage + (TotalCount % PageSize) - ;
}
} private int currentPage = ;//当前第几页
public int CurrentPage
{
get { return currentPage; }
set
{
currentPage = value;
lblCurrentPage.Text = currentPage.ToString();
}
} public int PageNum
{
get
{
int pageNum = ;//总的页数
if (PageSize > )
{
pageNum = (totalCount % PageSize == ) ? (totalCount / PageSize) : (totalCount / PageSize) + ;
}
return pageNum;
}
}
public delegate void LoadDataSouceDelegate(Hashtable condition);
public event LoadDataSouceDelegate LoadDataSouce;
private void lkbFirstPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{//首页
CurrentPage = ;
LoadDataSouce(condition);
lkbPrePage.Enabled = false;
if (TotalCount > PageSize)
{
lkbNextPage.Enabled = true;
}
} private void lkbPrePage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{//上一页
CurrentPage--;
LoadDataSouce(condition);
lkbNextPage.Enabled = true;
if (CurrentPage <= )
{
CurrentPage = ;
lkbPrePage.Enabled = false;
}
else
{
lkbPrePage.Enabled = true;
}
} private void lkbNextPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{//下一页
CurrentPage++;
LoadDataSouce(condition);
lkbPrePage.Enabled = true;
if (CurrentPage < PageNum)
{
lkbNextPage.Enabled = true;
}
else
{
CurrentPage = PageNum;
lkbNextPage.Enabled = false;
}
} private void lkbLastPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{//尾页
CurrentPage = PageNum;
LoadDataSouce(condition);
lkbNextPage.Enabled = false;
if (TotalCount > PageSize)
{
lkbPrePage.Enabled = true;
}
} private void btnGo_Click(object sender, EventArgs e)
{//跳转
if (!StringCommon.IsNumeric(txtWherePage.Text.Trim()))
{
MessageBox.Show("请输入数字");
return;
}
int goWherePage = StringCommon.ToInt32Value(txtWherePage.Text.Trim(),);
if (goWherePage <= || goWherePage > PageNum)
{
MessageBox.Show(string.Format("请输入1~{0}之间的数",PageNum));
return;
}
CurrentPage = goWherePage;
LoadDataSouce(condition);
if (CurrentPage <= )
{
lkbPrePage.Enabled = false;
}
else
{
lkbPrePage.Enabled = true;
}
if (CurrentPage < PageNum)
{
lkbNextPage.Enabled = true;
}
else
{
lkbNextPage.Enabled = false;
}
}
}
}
上一篇:Windows下caffe的配置和调用caffe库(二)


下一篇:机械硬盘与SSD固态硬盘性能的深度