GridView 72般絕技之與Linq喜結連理 之1,GridView自動分頁

來源:互聯網
上載者:User
看了園子裡的文章,有感而發,用Linq去實現。
http://blog.csdn.net/lipan2/archive/2008/09/14/2795707.aspx
此節主要關注Linq的分頁,原始碼在後邊附上(喜歡附上原始碼)

客服端代碼:

代碼        <asp:GridView ID="gridViewCourse" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" 
            PageSize="5" Width="614px" 
            onpageindexchanged="gridViewCourse_PageIndexChanged" 
            onpageindexchanging="gridViewCourse_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="Cno" HeaderText="學號" />
                <asp:BoundField DataField="Name" HeaderText="姓名" />
                <asp:BoundField DataField="Time" HeaderText="入學時間" />
                <asp:BoundField DataField="Money" HeaderText="錢" />
            </Columns>
        </asp:GridView

設定著三個屬性即可:
AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
PageSize="5"
服務端代碼:

代碼 public partial class GridNoCodePageForm : System.Web.UI.Page
    {
        private CourserService courseServcie;
        private int currentIndex = 1;
        private int pageSize = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                courseServcie = new CourserService();
                List<Course> courseList = courseServcie.FindAllList();
                if (courseList.Count == 0)
                    MessageBox.Show("沒有資料了!");
                this.gridViewCourse.DataSource = courseList;
                this.gridViewCourse.DataBind();
            }
        }

        private void BindSource(int pageIndex)
        {
            courseServcie = new CourserService();
            List<Course> courseList = courseServcie.FindList(pageIndex, pageSize);
            if (courseList.Count == 0)
                MessageBox.Show("沒有資料了!");
            this.gridViewCourse.DataSource = courseList;
            this.gridViewCourse.DataBind();
        }

        protected void gridViewCourse_PageIndexChanged(object sender, EventArgs e)
        {
        }

        protected void gridViewCourse_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            int currentIndex = e.NewPageIndex;
            BindSource(currentIndex);
        }

實現資料的綁定:以及在點擊“2”數位時候處理的代碼。這種綁定資料的方法不好之處在於,只要繫結資料大於5條時,才顯示
有數字頁。而且,Linq 的分頁也同樣有問題。它是查詢出來所有的學生資訊,然後再用Skip和Take方法實現分頁的。急切需要網友斧正。

代碼        public List<Course> FindList(int pageIndex, int pageSize)
        {
            pageIndex = pageIndex * pageSize - pageSize;
            List<Course> objList = new List<Course>();
            Table<Course> couse = context.GetTable<Course>();
            var courseQuery =
                from couseItem in couse
                orderby couseItem.Cno
                select couseItem;
            var pageQuery = courseQuery.Skip<Course>(pageIndex).Take<Course>(pageSize);
            foreach (Course cou in pageQuery)
            {
                objList.Add(cou);
            }
            return objList;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.