asp.Netの1つのlinqページング実装コード

5284 ワード

LInqページング
 
  
testDataContext dc = new testDataContext();
public string GetPageNum(GridView GridViewName, int pagesize, IQueryable sql)
{
int page;
if (HttpContext.Current.Request.QueryString["page"] != null)
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
else
page = 1;
//var sql = from o in dc.test select o;
int total = sql.Count();//
var sqls = sql.Skip((page - 1) * pagesize).Take(pagesize);
GridViewName.DataSource = sqls;
GridViewName.DataBind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//
if (pagesize != 0)
{
allpage = (total / pagesize);
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//
//
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } // , 1 1
if (allpage < endcount) { endcount = allpage; } // +5 ,
pagestr = " " + allpage + "     ";
pagestr += page > 1 ? "トップページ   のページ" : " ";
// , ,
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "  " + i + "" : "  " + i + "";
}
pagestr += page != allpage ? "   のページ   のページ" : " ";
return pagestr;
}

label 1を呼び出す.Test=GetPageNum(コントロール名、各ページにバー数、linqクエリ文を表示)
一般ページング
 
  
public static string GetPageNum(DataTable ds, DataList datalistname, int pagesize)
{
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.DefaultView;
objPds.AllowPaging = true;
int total = ds.Rows.Count;
objPds.PageSize = pagesize;
int page;
if (HttpContext.Current.Request.QueryString["page"] != null)
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
else
page = 1;
objPds.CurrentPageIndex = page - 1;
datalistname.DataSource = objPds;
datalistname.DataBind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//
if (pagesize != 0)
{
allpage = (total / pagesize);
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//
//
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } // , 1 1
if (allpage < endcount) { endcount = allpage; } // +5 ,
pagestr = " " + allpage + "     ";
pagestr += page > 1 ? "トップページ   のページ" : " ";
// , ,
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "  " + i + "" : "  " + i + "";
}
pagestr += page != allpage ? "   のページ   のページ" : " ";
return pagestr;
}

label 1を呼び出す.Test=GetPageNum(datatable、コントロール名、各ページにバー数を表示)