最近我们做了一个项目,其中用到DataGrid的分页,于是我编写了一个DataGrid的分页的类,主要原理就是利用此类返回一个DataTable,然后把返回的DataTable与DataGrid绑定。
因为我们做的是ASP.NET + ORACLE,所以这个类有一些ORACLE的成分,不过,有基础的同事一看就会明白的。这个类写的很简单,大家可以再此基础上进一步开发,如有需要源码的跟我联系。
里面牵扯到一些项目的东西,没时间改了,我想大家会看明白的。
代码:
using System;
using System.Data;
using System.Data.OracleClient;
using System.Web.UI.WebControls;
using dictionary;
namespace dictionary
{
/// <summary>
/// PageSelect 用于对于页面上的DataGrid的数据分页
/// </summary>
public class PageSelect
{
//dt 保存数据表
private DataTable dt = new DataTable();
//pageCount 为页面的总页数
private int pageCount;
//max Rec 为数据表的纪录总数
private int maxRec;
//pageSize 为每页中包含的纪录数
private int pageSize = 15;
//currentPage 为当前的页号
private int currentPage;
//recNo 为当页的最大纪录号
private int recNo;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="tableName">要查询的表名</param>
public PageSelect( string tableName )
{
//OracleConnection con = new OracleConnection( DataTrans.DBCONSTR );
//OracleDataAdapter da = new OracleDataAdapter( "Select * FROM " + tableName + " ORDER BY ID DESC", con );
//DataSet ds = new DataSet();
//da.Fill( ds, tableName );
//dt = ds.Tables[0];
dt.Columns.Add( "name", Type.GetType( "System.String" ) );
dt.Columns.Add( "age", Type.GetType( "System.Int32" ) );
for( int i = 1; i <= 50 ; i++ )
{
System.Data.DataRow dataRow = dt.NewRow();
dataRow["name"] = "name" + i.ToString();
dataRow["age"] = i;
dt.Rows.Add( dataRow );
}
maxRec = dt.Rows.Count;
pageCount = maxRec / pageSize;
if( ( maxRec % pageSize ) > 0 )
pageCount++;
}
public PageSelect( DataSet adDs )
{
dt = adDs.Tables[0];
maxRec = dt.Rows.Count;
pageCount = maxRec / pageSize;
if( ( maxRec % pageSize ) > 0 )
pageCount++;
}
private DataTable LoadPage()
{
int startRec, endRec;
DataTable dtTemp = dt.Clone();
if( currentPage == pageCount )
endRec = maxRec;
else
endRec = pageSize * currentPage;
startRec = recNo;
if( dt.Rows.Count > 0 )
{
for( int i = startRec; i < endRec; i++ )
{
dtTemp.ImportRow( dt.Rows[i] );
recNo++;
}
}
else
{
dtTemp = null;
}
return dtTemp;
}
public DataTable GetPageData()
{
return dt;
}
public DataTable GetFirstPage()
{
currentPage = 1;
recNo = 0;
return LoadPage();
}
public DataTable GetPreviousPage()
{
currentPage--;
if( currentPage == 0 )
{
currentPage = 1;
recNo = 0;
}
else
recNo = pageSize * ( currentPage - 1 );
return LoadPage();
}
public DataTable GetNextPage()
{
currentPage++;
if( currentPage > pageCount )
{
currentPage = pageCount;
recNo = pageSize * ( pageCount - 1 );
}
return LoadPage();
}
public DataTable GetLastPage()
{
currentPage = pageCount;
recNo = pageSize * ( pageCount - 1 );
return LoadPage();
}
public DataTable GetSetPage( int pageIndex )
{
if( pageIndex > 0 && pageIndex <= pageCount )
{
currentPage = pageIndex;
recNo = pageSize * ( currentPage - 1 );
return LoadPage();
}
else
{
recNo = pageSize * ( currentPage - 1 );
return LoadPage();
};
}
public void GetPageInfor( Label lblRecordCount, TextBox tbCurrentPage, Label lblPageCount )
{
lblRecordCount.Text = maxRec.ToString();
tbCurrentPage.Text = currentPage.ToString();
lblPageCount.Text = pageCount.ToString();
}
}
}
浙公网安备 33010602011771号