首页 新闻 会员 周边 捐助

datagridview分页显示问题

0
悬赏园豆:10 [已解决问题] 解决于 2010-08-06 17:05

c#,数据库表的内容显示在datagridview控件中,当数据太多一起显示麻烦。打算分页。每100条显示在一页中。添加2个按钮。一个显示下一页,就是当前数据的后面100条数据。另一个是textbox,直接定位到某一页。想看第几页就输入几。网上的代码有有点乱。有谁清楚该怎样弄吗?

代码苦行僧的主页 代码苦行僧 | 菜鸟二级 | 园豆:311
提问于:2010-07-27 16:16
< >
分享
最佳答案
1

这个是我自己写的个分页控件。效果入下图、希望对你有帮助。




[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);
}

收获园豆:5
HUHU慈悲 | 大侠五级 |园豆:9973 | 2010-08-06 08:43
其他回答(1)
0

WinForm下的吧?

其实和WEB里的分页是一样的,重要的是要搞清楚怎么来高效分页。高效分类可以有很多方法,可以查查。

其实在WinForm里实现更简单,没有刷新,在当前Form里定义几个变量,用于存储当前页,和每页多少条,和共页数等。

然后Form加载时,为第一页,上一页、下一页的代码就很简单了,只是+1,-1,然后根据页数这些信息从数据库中查出来,重新绑定给datagridview就行了。

可以参考它的UI执行那一块,http://www.cnblogs.com/sayu115/archive/2007/10/12/922115.html。

分页实现可以自己写。

收获园豆:5
Astar | 园豆:40805 (高人七级) | 2010-07-27 16:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册