各位有比較好的datagrid自定義分頁程序嗎?100分相送.

nnh 2003-08-25 09:01:36
各位有比較好的datagrid自定義分頁程序嗎?100分相送.
...全文
39 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
chmodyou 2003-08-25
  • 打赏
  • 举报
回复
做成一个控件,然后调用它:
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" border="0" class="PageTable">
<TR>
<TD class="NormalBold">
<asp:Image id="Image1" runat="server" ImageUrl="~/images/green_arrow_sml.gif"></asp:Image>每页
<asp:dropdownlist id="_ddlsPageCount" runat="server"></asp:dropdownlist>项
<asp:label id="_lbPageMsg" runat="server"></asp:label> 
<asp:button id="_btnFirst" runat="server" CssClass="PageGideButton" Text="9" CommandName="2"></asp:button><asp:button id="_btnPrev" runat="server" CssClass="PageGideButton" Text="7" CommandName="1"></asp:button><asp:label id="_lbPageNo" runat="server"></asp:label><asp:button id="_btnNext" runat="server" CssClass="PageGideButton" Text="8" CommandName="-1"></asp:button><asp:button id="_btnLast" runat="server" CssClass="PageGideButton" Text=":" CommandName="-2"></asp:button>
<asp:Label id="_lbDataGridArr" runat="server" Visible="False"></asp:Label>
</TD>
</TR>
</TABLE>
</FONT>

.cs代码:
protected System.Web.UI.WebControls.DropDownList _ddlsPageCount;
protected System.Web.UI.WebControls.Label _lbPageMsg;
protected System.Web.UI.WebControls.Button _btnFirst;
protected System.Web.UI.WebControls.Button _btnPrev;
protected System.Web.UI.WebControls.Label _lbPageNo;
protected System.Web.UI.WebControls.Button _btnNext;
protected System.Web.UI.WebControls.Label _lbDataGridArr;
protected System.Web.UI.WebControls.Button _btnLast;

public delegate void ClickedHandler();
public event ClickedHandler BeforNavigate;
public event ClickedHandler EndNavigate;

public int RowCount=0;
protected System.Web.UI.WebControls.Image Image1;
private short PageSize=0;

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack==false)
{
for(int i=1;i<=20;i++)
_ddlsPageCount.Items.Add(new ListItem(i.ToString(),i.ToString()));

_ddlsPageCount.SelectedIndex=PageSize-1;
}
}

public void GetPageInfo(string sDataGrid,short iPageSize)
{
_lbDataGridArr.Text=sDataGrid;
DataGrid dg=(DataGrid)Parent.FindControl(_lbDataGridArr.Text);
if (dg!=null)
{
if (dg.DataKeyField=="" || dg.DataKeyField ==null)
_lbPageMsg.Text="控件未指定DataKeyField!";
else
{
//设置DataGrid分页属性
dg.AllowPaging =true;
dg.AllowCustomPaging =false;
dg.PageSize =iPageSize;
dg.PagerStyle.Visible=false;

PageSize =iPageSize;

//设置第一页
if (RowCount<dg.PageSize)
dg.CurrentPageIndex=0;

dg.DataBind();
RefreshPageInfo();
}
}
else
{
_lbPageMsg.Text ="无法获取"+sDataGrid;
_btnFirst.Enabled=false;
_btnPrev.Enabled =false;
_btnNext.Enabled =false;
_btnLast.Enabled =false;
}
}

private void _PageButton()
{
//导航按钮状态
_btnFirst.Enabled=true;
_btnPrev.Enabled=true;
_btnNext.Enabled=true;
_btnLast.Enabled =true;

DataGrid dg=(DataGrid)Parent.FindControl(_lbDataGridArr.Text);

if (dg!=null)
{
if (dg.CurrentPageIndex==0)
{
_btnFirst.Enabled=false;
_btnPrev.Enabled=false;
}
if (dg.CurrentPageIndex==(dg.PageCount -1))
{
_btnNext.Enabled=false;
_btnLast.Enabled =false;
}
}
}

public void RefreshPageInfo()
{
//设置页面信息
DataGrid dg=(DataGrid)Parent.FindControl(_lbDataGridArr.Text);

if (dg!=null)
{
_lbPageMsg.Text="共" + dg.PageCount.ToString() + "页" + RowCount.ToString() + "条记录 ";
_lbPageNo.Text=Convert.ToString(dg.CurrentPageIndex+1);
_PageButton();
}
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// 设计器支持所需的方法 - 不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this._btnFirst.Click += new System.EventHandler(this._PageNav);
this._btnPrev.Click += new System.EventHandler(this._PageNav);
this._btnNext.Click += new System.EventHandler(this._PageNav);
this._btnLast.Click += new System.EventHandler(this._PageNav);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


private void _SetPage(int iPageSize,short iPageCmd)
{
DataGrid dg=(DataGrid)Parent.FindControl(_lbDataGridArr.Text);

if (dg!=null)
{
//触发动作开始事件
if (BeforNavigate!=null)
BeforNavigate();

if (dg.PageSize ==iPageSize)
{
switch(iPageCmd)
{
case 2:
dg.CurrentPageIndex=0;
break;
case 1:
if (dg.CurrentPageIndex>0)
dg.CurrentPageIndex--;
break;
case -1:
if (dg.CurrentPageIndex<(dg.PageCount-1))
dg.CurrentPageIndex++;
break;
case -2:
dg.CurrentPageIndex=(dg.PageCount-1);
break;
default:
dg.CurrentPageIndex=0;
break;
}
}
else
{
dg.PageSize=iPageSize;
dg.CurrentPageIndex =0;
}

//设置第一页
if (RowCount<dg.PageSize)
dg.CurrentPageIndex=0;

dg.DataBind();
RefreshPageInfo();
//触发动作结束事件
if (EndNavigate!=null)
EndNavigate();

}
}

private void _PageNav(object sender, System.EventArgs e)
{
short iPageCmd=Convert.ToInt16(((Button)sender).CommandName.Trim());
int iPageSize=Int32.Parse(_ddlsPageCount.SelectedItem.Value);
_SetPage(iPageSize,iPageCmd);
}
}

在另一页面或控件中调用此控件,并赋初值:
         private const short FirstPage = 2;
private const short PrevPage = 1;
private const short NextPage = -1;
private const short LastPage = -2;
         private const short PageSize=15;//默认显示页数

private void Page_Load(object sender, System.EventArgs e)
{

if (Page.IsPostBack == false)
{

DataGrid1.DataKeyField="ID";

BindData();//邦定数据

DataGridNavigateBar.GetPageInfo("DataGrid1",PageSize);


}
}
private void InitializeComponent()
{
this.DataGridNavigateBar.BeforNavigate +=new MViolation.DesktopModules.Common.DataGridNavigateBar.ClickedHandler(this.BindData);
}
nnh 2003-08-25
  • 打赏
  • 举报
回复
謝謝xrascal(横刀夺爱) , acewang(**^o^**) 你們的代碼,我看了,不符合我想要的.我所要的是要顯示那些記錄就綁定那些記錄.請問誰有這樣的自定義分頁程序????
bpy 2003-08-25
  • 打赏
  • 举报
回复
楼上大师的作品:http://webdiyer.europe.webmatrixhosting.net/aspnetpager.txt
很有代表性
webdiyer 2003-08-25
  • 打赏
  • 举报
回复
试试这个控件,有最新的源代码,谢谢!
http://www.webdiyer.com
acewang 2003-08-25
  • 打赏
  • 举报
回复
先定义ASPX页面,注意AllowCustomPaging要设为"False":


<body>
<form id="DictList" method="post" runat="server">
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" border="1">
<TR>
<td bgColor="#c0c000">信息:<FONT face="Arial" color="#ffffff">数据维护</FONT>
</td>
</TR>
<tr>
<td><FONT face="宋体"></FONT></td>
</tr>
<tr>
<td><asp:datagrid id="MyDataGrid" runat="server" Width="100%" PageSize="20" AllowPaging="True" AutoGenerateColumns="False" DataKeyField="FDictid">
<SelectedItemStyle BackColor="#FFC080"></SelectedItemStyle>
<HeaderStyle BackColor="#C0C000"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="选择" HeaderText="选择" CommandName="Select">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" Width="8%"></HeaderStyle>
<ItemStyle Font-Bold="True" HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:BoundColumn DataField="FDictID" SortExpression="FDictID asc" HeaderText="标识号">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNameCn" SortExpression="FNameCn asc" HeaderText="名称">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNameEn" SortExpression="FNameEn asc" HeaderText="英文名称">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNote" SortExpression="FNote asc" HeaderText="描叙">
<HeaderStyle Width="47%"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:datagrid></td>
</tr>
</TABLE>
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" bgColor="#ff9966" border="1">
<TR>
<td align="right"><asp:linkbutton id="btnFirst" runat="server" CommandArgument="fist">首页</asp:linkbutton>  
<asp:linkbutton id="btnPrev" runat="server" Width="36px" CommandArgument="prev">上一页</asp:linkbutton>  
<asp:linkbutton id="btnNext" runat="server" CommandArgument="next">下一页</asp:linkbutton>  
<asp:linkbutton id="btnLast" runat="server" CommandArgument="last">末页</asp:linkbutton>  
<asp:label id="lblCurrentIndex" runat="server"></asp:label>/<asp:label id="lblPageCount" runat="server"></asp:label>  
跳转到<asp:TextBox id="txtGoPage" runat="server" Width="30px" CssClass="textbox"></asp:TextBox>
<asp:Button id="btnGo" runat="server" Text="GO" CssClass="button" Width="29px"></asp:Button></td>
</TR>
</TABLE>
</form>







codebehind主要功能部分代码:



private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);

if (!IsPostBack)
BindGrid();
}



public void BindGrid()
{
string strSql ="SELECT * FROM t_dict ";
SqlDataAdapter myCommand = new SqlDataAdapter(strSql, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "t_dict");
MyDataGrid.DataSource=ds.Tables["t_dict"].DefaultView;
MyDataGrid.DataBind();
ShowStatsPage();
}

private void PagerButtonClick(object sender, System.EventArgs e)
{
//获得LinkButton的参数值
String arg = ((LinkButton)sender).CommandArgument;

switch(arg)
{
case ("next"):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"):
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("fist"):
MyDataGrid.CurrentPageIndex=0;
break;
case ("last"):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
//本页值
MyDataGrid.CurrentPageIndex = Convert.ToInt32(arg);
break;
}
BindGrid();
}

void ShowStatsPage()
{
//显示页面信息
lblCurrentIndex.Text = "[<font color="blue">当前为第:" + ((int)MyDataGrid.CurrentPageIndex+1) + "页</font>]";
lblPageCount.Text = "[<font color="blue">共:" + MyDataGrid.PageCount + "页</font>]";
}



private void MyDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//处理按下数字的方法
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}



private void btnGo_Click(object sender, System.EventArgs e)
{
//页面直接跳转的代码
if(txtGoPage.Text.Trim()!="")
{
int PageI=Int32.Parse(txtGoPage.Text.Trim())-1;
if (PageI >=0 && PageI < (MyDataGrid.PageCount))
MyDataGrid.CurrentPageIndex = PageI ;
}
BindGrid();
}
//----------------------翻页代码结束


guoyan19811021 2003-08-25
  • 打赏
  • 举报
回复
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=B12283DE-DB20-4322-ACCC-12724442808A
暗石绿 2003-08-25
  • 打赏
  • 举报
回复
<HTML>
<HEAD>
<title></title>
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<Script Language="C#" Runat="Server">
/*
Modified By Blur
Support .Net Framework Beta 2
*/
SqlConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;//分页之用
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;
//连接语句
string MyConnString = "User ID=qxw;Password=45052182;initial catalog=rascal;data source=www";
MyConn = new SqlConnection(MyConnString);
//打开数据库连接
MyConn.Open();

//第一次请求执行
if(!Page.IsPostBack)
{
//计算总共有多少记录
RecordCount = CalculateRecord();
//计算总共有多少页
//取整
PageCount = RecordCount/PageSize;
if (RecordCount%PageSize > 0)
PageCount = PageCount + 1;
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
ViewState["PageCount"] = PageCount;
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//绑定
ListBind();
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from fenye";
SqlCommand MyComm = new SqlCommand(strCount,MyConn);
SqlDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}

ICollection CreateSource()
{

int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from fenye";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"fenye");
return ds.Tables["fenye"].DefaultView;
}
public void ListBind()
{
fenye.DataSource = CreateSource();
fenye.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(PageCount==0)
{
lblCurrentPage.Text = "0";
lbnNextPage.Enabled = false;
lbnPrevPage.Enabled = false;
}
else
{
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
}

public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}

ViewState["PageIndex"] = CurrentPage;

ListBind();

}
</Script>
</HEAD>
<body>
<form runat="server" ID="Form1">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
<asp:DataList id="fenye" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="Gainsboro" EditItemStyle-BackColor="yellow">
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"fenyeID") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
</form>
</body>
</HTML>
houjianxun 2003-08-25
  • 打赏
  • 举报
回复
配合一下这个用试试

http://expert.csdn.net/Expert/topic/2184/2184233.xml?temp=.9492456
nnh 2003-08-25
  • 打赏
  • 举报
回复
還有沒有呀??
chenyu112 2003-08-25
  • 打赏
  • 举报
回复
这不是已经很多了?
菁菁报表 2003-08-25
  • 打赏
  • 举报
回复
液态长了把
nnh 2003-08-25
  • 打赏
  • 举报
回复
up

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧