Introduction
This example demonstrates how to create pagination using LINQ in C# desktop applications.
Implementation
So, lets start implementation of our example.
Step 1 Open your Visual Studio and create a new project.
Step 2 Now, design a form like this.
Step 3 Create a class with the name SampleData.cs and write the following code in it.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication1
  6. {
  7. public class SampleData
  8. {
  9. public int ID;
  10. public string Name;
  11. public string Age;
  12. public string Address;
  13. public SampleData()
  14. {
  15. }
  16. }
  17. }
Step 4 Write the following code within class.
  1. List<SampleData> _sampleDataList = new List<SampleData>();
Step 5 Write the following code at the time of constuctor creation.
  1. int currentPage = 0;
  2. int currentSize = 10;
  3. public SampleForm()
  4. {
  5. InitializeComponent();
  6. //i Created a dummy data to use for pagination
  7. CreateDummy();
  8. currentPage = 0;
  9. Paging(currentPage, currentSize);
  10. }
Step 6 Now, create the following function.
  1. private void Paging(int pagenum, int pagesize)
  2. {
  3. if (currentPage < 0) { currentPage = 0; return; }
  4. flowLayoutPanel1.Controls.OfType<Label>().Where(e => e.Tag.ToString() != (pagenum + 1).ToString())
  5. .ToList().ForEach((element) =>
  6. {
  7. element.ForeColor = Color.Black;
  8. });
  9. flowLayoutPanel1.Controls.OfType<Label>().Where(e => e.Tag.ToString() == (pagenum + 1).ToString())
  10. .First().ForeColor = Color.Red;
  11. textBox1.Text = "Page " + (pagenum + 1) + " of " + (int)(_sampleDataList.Count / pagesize);
  12. var products = from p in _sampleDataList.Skip(pagenum * pagesize).Take(pagesize)
  13. select new { ID = p.ID, Name = p.Name, Age = p.Age, Address = p.Address };
  14. dataGridView1.DataSource = products.ToList();
  15. }
  1. private void CreateDummy()
  2. {
  3. Enumerable.Range(1, 100).ToList().ForEach((element) =>
  4. {
  5. SampleData s = new SampleData();
  6. s.ID = element;
  7. s.Name = "FullName" + element;
  8. s.Age = new Random().Next().ToString();
  9. s.Address = "Davao";
  10. _sampleDataList.Add(s);
  11. });
  12. Enumerable.Range(1, (_sampleDataList.Count() / currentSize)).ToList().ForEach((element) =>
  13. {
  14. Label lbl = new System.Windows.Forms.Label();
  15. lbl.AutoSize = true;
  16. lbl.Location = new System.Drawing.Point(3, 0);
  17. lbl.Name = "lbl" + element;
  18. lbl.Size = new System.Drawing.Size(61, 13);
  19. lbl.Text = element.ToString();
  20. lbl.Tag = element;
  21. lbl.Click += new EventHandler(lbl_Click);
  22. this.flowLayoutPanel1.Controls.Add(lbl);
  23. });
  24. }
Step 7 Write the following code in click event of button1,2,3 and button4.
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. currentPage = 0;
  4. Paging(currentPage, currentSize);
  5. }
  6. private void button2_Click(object sender, EventArgs e)
  7. {
  8. currentPage = (currentPage - currentSize) < 0 ? (currentPage - 1) : 0;
  9. Paging(currentPage, currentSize);
  10. }
  11. private void button3_Click(object sender, EventArgs e)
  12. {
  13. currentPage = ((currentPage + 1) * currentSize) < _sampleDataList.Count() ?
  14. (currentPage + 1) : currentPage;
  15. Paging(currentPage, currentSize);
  16. }
  17. private void button4_Click(object sender, EventArgs e)
  18. {
  19. currentPage = (_sampleDataList.Count() / currentSize) - 1;
  20. Paging(currentPage, currentSize);
  21. }
Step 8 Run the project and you can see the following output on your screen.

Summary
In a large set of data, pagination is a reliable technique to find the record from the record list.