最早写的自定义分页
/// <summary>
/// 返还分好页的数据源
/// </summary>
/// <param name="wherestr">条件</param>
/// <param name="tablename">表名</param>
/// <param name="prime_key">主键</param>
/// <param name="orderzd">排序字段</param>
/// <param name="currpageindex">当前页</param>
/// <param name="mydtsouce">数据源</param>
/// <param name="paddnum">页码</param>
/// <param name="myorders">排序方式</param>
/// <returns></returns>
public DataTable getpsdsouce(string wherestr, string tablename, string prime_key, string orderzd, int currpageindex, DataTable mydtsouce, int paddnum, string myorders)
{
DataTable dtsource = new DataTable();
dtsource = mydtsouce;
int paddingnum = paddnum;
if (dtsource != null)
{
//startindex = Convert.ToInt32(dtsource.Rows[0][prime_key]);//最小ID
//endindex = Convert.ToInt32(dtsource.Rows[dtsource.Rows.Count - 1][prime_key]);//最大ID
if (dtsource.Rows.Count <= paddingnum)
{
if (wherestr == "")
{
if (orderzd == "")
{
sql = "select top " + paddingnum + " * from " + tablename;//house_recommendationlist
}
else
{
sql = "select top " + paddingnum + " * from " + tablename + " order by " + orderzd + " " + myorders;
}
}
else
{
if (orderzd == "")
{
sql = "select top " + paddingnum + " * from " + tablename;//house_recommendationlist
}
else
{
sql = "select top " + paddingnum + " * from " + tablename + " where " + wherestr + " order by " + orderzd + " " + myorders;
}
}
}
else
{
if ((dtsource.Rows.Count - ((currpageindex - 1) * paddingnum)) <= paddingnum)
{
if (wherestr.Length == 0)
{
if (orderzd.Length == 0)
{
sql = "select * from " + tablename + " where " + prime_key + " >= " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key];
}
else
{
if (myorders.ToString() == "asc")
{
sql = "select * from " + tablename + " where " + prime_key + " >= " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " order by " + orderzd + " " + myorders;
}
else
{
sql = "select * from " + tablename + " where " + prime_key + " <= " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " order by " + orderzd + " " + myorders;
}
}
}
else
{
if (orderzd.Length == 0)
{
if (myorders.ToString() == "asc")
{
sql = "select * from " + tablename + " where " + wherestr + " and " + prime_key + " >= " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key];
}
}
else
{
if (myorders.ToString() == "asc")
{
sql = "select * from " + tablename + " where " + wherestr + " and " + prime_key + " >= " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " order by " + orderzd + " " + myorders;
}
else
{
sql = "select * from " + tablename + " where " + wherestr + " and " + prime_key + " <= " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " order by " + orderzd + " " + myorders;
}
}
}
}
else
{
if (wherestr.Length == 0)
{
if (orderzd.Length == 0)
{
sql = "select * from " + tablename + " where " + prime_key + " between " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " and " + dtsource.Rows[(paddingnum * currpageindex) - 1][prime_key];
}
else
{
if (myorders.ToString() == "asc")
{
sql = "select * from " + tablename + " where " + prime_key + " between " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " and " + dtsource.Rows[(paddingnum * currpageindex) - 1][prime_key] + " order by " + orderzd + " " + myorders;
}
else
{
sql = "select * from " + tablename + " where " + prime_key + " between " + dtsource.Rows[(paddingnum * currpageindex) - 1][prime_key] + " and " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " order by " + orderzd + " " + myorders;
}
}
}
else
{
if (orderzd.Length == 0)
{
sql = "select * from " + tablename + " where " + wherestr + " and " + prime_key + " between " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " and " + dtsource.Rows[(paddingnum * currpageindex) - 1][prime_key];
}
else
{
if (myorders.ToString() == "asc")
{
sql = "select * from " + tablename + " where " + wherestr + " and " + prime_key + " between " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " and " + dtsource.Rows[(paddingnum * currpageindex) - 1][prime_key] + " order by " + orderzd + " " + myorders;
}
else
{
sql = "select * from " + tablename + " where " + wherestr + " and " + prime_key + " between " + dtsource.Rows[(paddingnum * currpageindex) - 1][prime_key] + " and " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " order by " + orderzd + " " + myorders;
}
}
}
//sql = "select * from " + tablename + " where " + prime_key + " between " + dtsource.Rows[paddingnum * (currpageindex - 1)][prime_key] + " and " + dtsource.Rows[(paddingnum * currpageindex) - 1][prime_key];
}
}
dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
}
return dt;
}