Linq to sql 分頁

世紀緣發表於2016-03-22
//主要就是Skip() 和 Take()   
     private void Page()
        {
            LinqDataContext cxt = new LinqDataContext();

            data = from r in cxt.Customers
                   select r;

            //Button

            pageCount = int.Parse(Math.Ceiling(data.Count() * 1.0 / 10).ToString());

            for (int i = 1; i <= pageCount; i++)
            {
                Button but = new Button();
                but.Text = i.ToString();
                but.Size = new System.Drawing.Size(45, 30);
                but.Location = new Point((i - 1) * 50, 5);
                but.Click += but_Click;

                this.panelBut.Controls.Add(but);
            }
        }

        void but_Click(object sender, EventArgs e)
        {
            Button but = (Button)sender;

            PageData(int.Parse(but.Text));
        }

        private void PageData(int currentPage)
        {
            this.dataGridView1.DataSource = data.Skip((currentPage - 1) * 10).Take(10).ToList();
        }

相關文章