DataRow collection paging
This sample demonstrate how to create PagedList<T> collection manually.
Total Records:100, display 91 to 100
| Order ID | Customer Name |
|---|---|
| 91 | Customer_91 |
| 92 | Customer_92 |
| 93 | Customer_93 |
| 94 | Customer_94 |
| 95 | Customer_95 |
| 96 | Customer_96 |
| 97 | Customer_97 |
| 98 | Customer_98 |
| 99 | Customer_99 |
| 100 | Customer_100 |
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PagedList<DataRow>>" %>
<%@ Import Namespace="Webdiyer.WebControls.Mvc"%>
<%@ Import Namespace="System.Data"%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
DataRow collection paging
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<style type="text/css">
.pagerItem a{text-decoration:none}
.redbold{color:Red;font-weight:bold}
</style>
<h2>DataRow collection paging</h2>
<div>This sample demonstrate how to create PagedList<T> collection manually.</div><br />
<div>Total Records:<%=Model.TotalItemCount %>, display <%=Model.StartRecordIndex %>
to <%=Model.EndRecordIndex %></div>
<table width="98%">
<tr><th>Order ID</th><th>Customer Name</th></tr>
<%foreach(DataRow dr in Model)
{
%>
<tr><td><%=dr["id"] %></td><td><%=dr["CustomerName"] %></td></tr>
<%
} %>
</table>
<%=Html.Pager(Model, new PagerOptions { PageIndexParameterName = "id",
CssClass = "pagerItem", FirstPageText = "<<", LastPageText = ">>", PrevPageText = "<",
NextPageText = ">", CurrentPagerItemWrapperFormatString = "<span class=\"redbold\">{0}</span>" })%>
</asp:Content>
public ActionResult DataRowsPaging(int? id)
{
var dt = new DataTable("Orders");
dt.Columns.Add(new DataColumn("Id", typeof(int)));
dt.Columns.Add(new DataColumn("CustomerName", typeof(string)));
for (int i = 1; i <= 100; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = "Customer_" + i;
dt.Rows.Add(dr);
}
int pageSize = 10;
int pageIndex = id ?? 1;
PagedList<DataRow> pl = new PagedList<DataRow>(dt.Select(), pageIndex, pageSize);
//or: PagedList<DataRow> pl =new PagedList<DataRow>(dt.Select("Id>"+(pageIndex-1)*pageSize+" and
Id<="+pageIndex*pageSize), pageIndex, pageSize,dt.Rows.Count);
return View(pl);
}