DataRow collection paging
This sample demonstrate how to create PagedList<T> collection manually.
Total Records:100, display 1 to 10
| Order ID | Customer Name |
|---|---|
| 1 | Customer_1 |
| 2 | Customer_2 |
| 3 | Customer_3 |
| 4 | Customer_4 |
| 5 | Customer_5 |
| 6 | Customer_6 |
| 7 | Customer_7 |
| 8 | Customer_8 |
| 9 | Customer_9 |
| 10 | Customer_10 |
<%@ 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);
}