先进行自定分页:
public DataGrid BindGrid(DataGrid mDataGrid,string sSql,int iPageIndex)
{
// 在此处放置用户代码以初始化页面 SqlConnection cnn = new SqlConnection(sConnString);
SqlDataAdapter myCommand = new SqlDataAdapter(sSql,cnn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "DataGrid");
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
if (iPageSize>0)
{
objPds.AllowPaging = true;
objPds.PageSize = iPageSize;
objPds.CurrentPageIndex = iPageIndex-1;
iCountRows = objPds.DataSourceCount;
iCountPages = objPds.PageCount; iCurrentPageIndex = iPageIndex; }
mDataGrid.DataSource=objPds;
mDataGrid.DataBind();
return mDataGrid;
}
public DataGrid BindGrid(DataGrid mDataGrid,string sSql,int iPageIndex)
{
// 在此处放置用户代码以初始化页面 SqlConnection cnn = new SqlConnection(sConnString);
SqlDataAdapter myCommand = new SqlDataAdapter(sSql,cnn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "DataGrid");
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
if (iPageSize>0)
{
objPds.AllowPaging = true;
objPds.PageSize = iPageSize;
objPds.CurrentPageIndex = iPageIndex-1;
iCountRows = objPds.DataSourceCount;
iCountPages = objPds.PageCount; iCurrentPageIndex = iPageIndex; }
mDataGrid.DataSource=objPds;
mDataGrid.DataBind();
return mDataGrid;
}
public string ShowPages()
{
string sTemp="";
string sTempPage="";
int iPrePage=0;
int iNextPage=0;
if (iCurrentPageIndex>1)
{
iPrePage=iCurrentPageIndex-1;
sTempPage=iPrePage.ToString(); sTemp+="<a href='?page="+ sTempPage "'>Prev</a> ";
} if (iCurrentPageIndex<iCountPages)
{
iNextPage=iCurrentPageIndex+1;
sTempPage=iNextPage.ToString();
sTemp+="<a href='?page="+ sTempPage "'>Next</a> ";
}
return sTemp;
}