Gridview使用LINQ分页例子

Linq的分页主要是使用了skip和take

关于skip 参看 http://msdn.microsoft.com/zh-cn/library/bb357513.aspx

关于take 参看http://msdn.microsoft.com/zh-cn/library/bb300906.aspx

另外说明下NorthWindDataContext 我是采用了Linq的类成长办法自动生成的

关于Linq的语法和介绍可以参看http://www.cnblogs.com/lovecherry/archive/2007/08/13/853754.html

上面介绍的还不错。其实如果你用过SQL,用过枚举类型会发现LINQ没啥,但是确实很省力,会发现以前的ORM工具在小型开发的时候直接用LINQ算了。



前台代码Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
        <br />
        <webdiyer:AspNetPager ID="Pager" runat="server" 
            PageIndexBoxType="DropDownList" ShowPageIndexBox="Always" SubmitButtonText="Go" 
            TextAfterPageIndexBox="" TextBeforePageIndexBox="转到" 
            CurrentPageButtonPosition="End" 
            CustomInfoHTML="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条" 
            FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" 
            onpagechanged="Pager_PageChanged" PrevPageText="上一页">
        </webdiyer:AspNetPager>
    
    </div>
    </form>
</body>
</html>

后台代码Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //得到所有的分页信息和预设的分页大小
            //pagesize来自girdview
            NorthWindDataContext context = new NorthWindDataContext();
            int total = (from custom in context.GetTable<Customers>()
                         select custom).Count();
            Pager.PageSize = GridView1.PageSize;
            Pager.RecordCount = total;

            gvDataBind(GridView1.PageSize, 1);

        }
    }

    protected void gvDataBind(int pagesize, int pageindex)
    {
        NorthWindDataContext context = new NorthWindDataContext();
        var customs = (from custom in context.GetTable<Customers>()
                       orderby custom.ContactName
                       select new
                       {
                           custom.ContactName,
                           custom.CustomerID,
                           custom.CompanyName,
                           custom.ContactTitle

                       }).Skip((pageindex - 1) * pagesize).Take(pagesize);
        GridView1.DataSource = customs;
        GridView1.DataBind();               

    }


    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = Pager.CurrentPageIndex;
        gvDataBind(GridView1.PageSize, Pager.CurrentPageIndex);
    }
}

 

posted @ 2013-03-20 15:25  xw2cc  阅读(211)  评论(0编辑  收藏  举报