这个是我自己写的个分页控件。效果入下图、希望对你有帮助。[ToolboxBitmap("ClubManager.WinControl.Resource.ico")]
public class PageNavigator : ToolStrip
{
const string FirstImage = "ClubManager.WinControl.MoveFirst.bmp";
const string PreviousImage = "ClubManager.WinControl.MovePrevious.bmp";
const string NextImage = "ClubManager.WinControl.MoveNext.bmp";
const string LastImage = "ClubManager.WinControl.MoveLast.bmp";
const string QueryImage = "ClubManager.WinControl.Query.bmp";
const string FirstTipText = "显示第一页";
const string PreviousTipText = "显示上一页";
const string NextTipText = "显示下一页";
const string LastTipText = "显示最后一页";
const string ToolTipText = "第 n 页";
const string LabelTipText = "总页数";
const string QueryTipText = "跳转到第 n 页(n->输入的数字)";
private ToolStripItem currentItem;
private ToolStripItem firtItem;
private ToolStripItem nextItem;
private ToolStripItem previousItem;
private ToolStripItem lastItem;
private ToolStripItem textItem;
private ToolStripItem labelItem;
private ToolStripItem labelItemPrefix;
private ToolStripItem labelItemEnd;
private ToolStripItem queryItem;
private int pageIndex;
static object pageIndexChangingKey = new object();
/// <summary>
/// 当前选项
/// </summary>
public ToolStripItem CurrentItem
{
get { return this.currentItem; }
}
/// <summary>
/// 支持分页
/// </summary>
private bool SupportPaging
{ get; set; }
/// <summary>
/// 数据源
/// </summary>
private DataTable DataSource
{ get; set; }
/// <summary>
/// 数据源记录总行数
/// </summary>
public int DataSourceCount
{ get; set; }
/// <summary>
/// 当前页索引
/// </summary>
public int PageIndex
{
get
{
return pageIndex + 1;
}
}
/// <summary>
/// 分页页记录行数
/// </summary>
[DefaultValue(10)]
public int PageSize
{
get;
set;
}
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
int iCount = DataSourceCount / PageSize;
if (DataSourceCount % PageSize > 0)
{ iCount = iCount + 1; }
return iCount;
}
}
private Bitmap CreateBitmap(string fullName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(fullName);
Bitmap bitmap = new Bitmap(stream, true);
bitmap.MakeTransparent(Color.Magenta);
return bitmap;
}
bool ValidateInput(string inputString)
{
Regex reg = new Regex("^[1-9]\\d*$");
Match match = reg.Match(inputString);
return match.Success;
}
public PageNavigator()
{
InitializingComponent();
this.KeyUp += delegate(object sender, KeyEventArgs e)
{
};
}
public void InitializingComponent()
{
firtItem = new ToolStripButton();
firtItem.Image = CreateBitmap(FirstImage);
firtItem.Name = "PageNavigatorMoveFirstItem";
firtItem.ToolTipText = FirstTipText;
previousItem = new ToolStripButton();
previousItem.Image = CreateBitmap(PreviousImage);
previousItem.Name = "PageNavigatorMovePreviousItem";
previousItem.ToolTipText = PreviousTipText;
nextItem = new ToolStripButton();
nextItem.Image = CreateBitmap(NextImage);
nextItem.Name = "PageNavigatorMoveNextItem";
nextItem.ToolTipText = NextTipText;
lastItem = new ToolStripButton();
lastItem.Image = CreateBitmap(LastImage);
lastItem.Name = "PageNavigatorMoveLastItem";
lastItem.ToolTipText = LastTipText;
textItem = new ToolStripTextBox();
textItem.Name = "PageNavigatorTextItem";
textItem.ToolTipText = ToolTipText;
textItem.Size = new Size(45, 20);
labelItemPrefix = new ToolStripLabel();
labelItemPrefix.Text = "/";
labelItemPrefix.Name = "PageNavigatorLabelPrefix";
labelItem = new ToolStripLabel();
labelItem.Text = this.pageIndex.ToString();
labelItem.Name = "PageNavigatorLabelItem";
labelItem.ToolTipText = LabelTipText;
labelItemEnd = new ToolStripLabel();
labelItemEnd.Text = " 页 ";
labelItemEnd.Name = "PageNavigatorLabelEnd";
queryItem = new ToolStripButton();
queryItem.Image = CreateBitmap(QueryImage);
queryItem.Name = "PageNavigatorQueryItem";
queryItem.ToolTipText = QueryTipText;
this.Items.AddRange(new ToolStripItem[] { firtItem, previousItem, nextItem, lastItem, textItem, labelItemPrefix, labelItem, labelItemEnd, queryItem });
}
public event PageIndexChangingEventHandler PageIndexChanging
{
add { Events.AddHandler(pageIndexChangingKey, value); }
remove { Events.RemoveHandler(pageIndexChangingKey, value); }
}
protected virtual void OnPageIndexChanging(PageIndexChangingEventArgs e)
{
var handler = Events[pageIndexChangingKey] as PageIndexChangingEventHandler;
if (handler != null)
{ handler(this, e); }
}
bool EnsureToolStripItemState(int index)
{
if (PageCount == 1)
{
this.firtItem.Enabled = false;
this.previousItem.Enabled = false;
this.nextItem.Enabled = false;
this.lastItem.Enabled = false;
}
else
{
if (index == 0)
{
this.firtItem.Enabled = false;
this.previousItem.Enabled = false;
this.nextItem.Enabled = true;
this.lastItem.Enabled = true;
}
else if ((index + 1) == PageCount)
{
;
this.firtItem.Enabled = true;
this.previousItem.Enabled = true;
this.nextItem.Enabled = false;
this.lastItem.Enabled = false;
}
}
return true;
}
protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
{
base.OnItemClicked(e);
this.currentItem = e.ClickedItem;
switch (e.ClickedItem.Name)
{
case "PageNavigatorMoveFirstItem":
if (PageIndex > 0)
{
pageIndex = 0;
}
break;
case "PageNavigatorMovePreviousItem":
if (PageIndex > 0)
{ pageIndex--; }
break;
case "PageNavigatorMoveLastItem":
if (PageIndex < PageCount)
{
pageIndex = PageCount - 1;
}
break;
case "PageNavigatorQueryItem":
ToolStripTextBox txt = this.Items["PageNavigatorTextItem"] as ToolStripTextBox;
if (txt != null)
{
if (!ValidateInput(txt.Text.Trim()))
{
MessageBox.Show("只能输入大于零的数字");
return;
}
int index = 0;
int.TryParse(txt.Text, out index);
index--;
if (0 <= index && index < PageCount)
{
pageIndex = index;
}
else
{ MessageBox.Show("数字超出范围"); }
}
break;
default:
if (PageIndex < PageCount)
{
pageIndex++;
}
break;
}
EnsureToolStripItemState(pageIndex);
PageIndexChangingEventArgs ee = new PageIndexChangingEventArgs(pageIndex);
OnPageIndexChanging(ee);
}
public void BindData()
{
this.textItem.Text = this.PageIndex.ToString();
this.labelItem.Text = this.PageCount.ToString();
EnsureToolStripItemState(pageIndex);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.DataSource = null;
}
base.Dispose(disposing);
}
WinForm下的吧?
其实和WEB里的分页是一样的,重要的是要搞清楚怎么来高效分页。高效分类可以有很多方法,可以查查。
其实在WinForm里实现更简单,没有刷新,在当前Form里定义几个变量,用于存储当前页,和每页多少条,和共页数等。
然后Form加载时,为第一页,上一页、下一页的代码就很简单了,只是+1,-1,然后根据页数这些信息从数据库中查出来,重新绑定给datagridview就行了。
可以参考它的UI执行那一块,http://www.cnblogs.com/sayu115/archive/2007/10/12/922115.html。
分页实现可以自己写。