Fork me on GitHub

MvcPager 分页示例 — DataRow集合分页

该示例演示使用DataAdapter填充DataTable,对DataRow集合进行分页。

该示例演示使用传统的ADO.NET数据访问技术调用sql server存储过程,使用DataAdapter填充DataTable,并调用PagedList构造函数生成PagedList<DataRow>的分页数据对象从面实现分页。

标题 作者 来源
延安市吴起县杨涛吴起热线
吴起县大力推进生态文明建设杨涛吴起政府网
我县文艺表演节目在《我要上春晚》栏目播出通知杨涛吴起政府网
吴起镇关于开展辖区内45岁以上居民免费体检的通知Webdiyer吴起政府网
关于在全镇范围内开展麻疹疫苗强化免疫活动的通知Webdiyer吴起热线
首页 上页 ... 2 3 4 5 6 7 8 9 10 11 下页 尾页 

View:

@model PagedList<system.data.datarow>
<table class="table table-striped table-bordered">
        <tr>
        <th>标题</th>
        <th>作者</th>
        <th>来源</th>
    </tr>
    @foreach (var dr in Model)
    {
        <tr><td>@dr["Title"]</td><td>@dr["Author"]</td><td>@dr["Source"]</td></tr>
    }
</table>
@Html.Pager(Model,new PagerOptions{PageIndexParameterName = "id",PagerItemTemplate = "{0}&nbsp;"})

Controller:

        public ActionResult DataRows(int id=1)
        {
            var pageSize = 5;
            var startIndex = (id - 1) * pageSize + 1;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString);
            SqlCommand cmd = new SqlCommand("USP_GetPagedArticleList", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@startIndex",SqlDbType.Int).Value=startIndex;
            cmd.Parameters.Add("@endIndex",SqlDbType.Int).Value=startIndex + pageSize;
            SqlParameter prmTotal = new SqlParameter("@totalItems", SqlDbType.Int);
            prmTotal.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(prmTotal);
            SqlDataAdapter adapter=new SqlDataAdapter(cmd);
            DataTable tbl=new DataTable("Articles");
            adapter.Fill(tbl);
            int totalItems = (int) prmTotal.Value; //要分页的总记录数
            //PagedList构造函数
            PagedList<datarow> arts=new PagedList<datarow>(tbl.Select(),id,pageSize,totalItems);
            return View(arts);
        }