Fork me on GitHub

MvcPager 分页示例 — DataRow集合分页

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

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

标题 作者 来源
吴起县文联《长征》文学季刊约稿启事杨涛吴起政府网
吴起县连续七届蝉联陕西县域经济社会发展十强县Webdiyer吴起政府网
吴起县教育局2012年招聘教师公告Webdiyer吴起热线
吴起退耕还林“还出”一片秀美山川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);
        }