日期:2009-05-16  浏览次数:20516 次

这里我实现了DataGrid的个性化分页,现在把代码贴出来朋友们参考,同时在这个功能的实现过程,参考了“飞刀”从国外翻译的一篇文章。


  先定义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="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;
}