How To Create Pagination Using LINQ In C#

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.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     public class SampleData  
  9.     {  
  10.         public int ID;  
  11.         public string Name;  
  12.         public string Age;  
  13.         public string Address;  
  14.   
  15.         public SampleData()  
  16.         {  
  17.         }  
  18.     }  
  19. }  
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.   
  4. public SampleForm()  
  5.    {  
  6.        InitializeComponent();  
  7.        //i Created a dummy data to use for pagination  
  8.        CreateDummy();          
  9.        currentPage = 0;  
  10.        Paging(currentPage, currentSize);  
  11.    }  
Step 6 Now, create the following function.
  1. private void Paging(int pagenum, int pagesize)  
  2.        {  
  3.   
  4.            if (currentPage < 0) { currentPage = 0; return; }  
  5.   
  6.            flowLayoutPanel1.Controls.OfType<Label>().Where(e => e.Tag.ToString() != (pagenum + 1).ToString())  
  7.                .ToList().ForEach((element) =>  
  8.                {  
  9.                    element.ForeColor = Color.Black;  
  10.                });  
  11.   
  12.            flowLayoutPanel1.Controls.OfType<Label>().Where(e => e.Tag.ToString() == (pagenum + 1).ToString())  
  13.                .First().ForeColor = Color.Red;  
  14.   
  15.   
  16.   
  17.            textBox1.Text = "Page " + (pagenum + 1) + " of " + (int)(_sampleDataList.Count / pagesize);  
  18.   
  19.            var products = from p in _sampleDataList.Skip(pagenum * pagesize).Take(pagesize)  
  20.                           select new { ID = p.ID, Name = p.Name, Age = p.Age, Address = p.Address };  
  21.   
  22.            dataGridView1.DataSource = products.ToList();  
  23.        }  
  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.   
  11.               _sampleDataList.Add(s);  
  12.           });  
  13.   
  14.           Enumerable.Range(1, (_sampleDataList.Count() / currentSize)).ToList().ForEach((element) =>  
  15.           {  
  16.               Label lbl = new System.Windows.Forms.Label();  
  17.               lbl.AutoSize = true;  
  18.               lbl.Location = new System.Drawing.Point(3, 0);  
  19.               lbl.Name = "lbl" + element;  
  20.               lbl.Size = new System.Drawing.Size(61, 13);  
  21.               lbl.Text = element.ToString();  
  22.               lbl.Tag = element;  
  23.   
  24.               lbl.Click += new EventHandler(lbl_Click);  
  25.   
  26.               this.flowLayoutPanel1.Controls.Add(lbl);  
  27.           });  
  28.       }  
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.   
  7.        private void button2_Click(object sender, EventArgs e)  
  8.        {  
  9.            currentPage = (currentPage - currentSize) < 0 ? (currentPage - 1) : 0;  
  10.            Paging(currentPage, currentSize);  
  11.        }  
  12.   
  13.        private void button3_Click(object sender, EventArgs e)  
  14.        {  
  15.            currentPage = ((currentPage + 1) * currentSize) < _sampleDataList.Count() ?  
  16.                (currentPage + 1) : currentPage;  
  17.            Paging(currentPage, currentSize);  
  18.        }  
  19.   
  20.        private void button4_Click(object sender, EventArgs e)  
  21.        {  
  22.            currentPage = (_sampleDataList.Count() / currentSize) - 1;  
  23.            Paging(currentPage, currentSize);  
  24.        }  
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.
Next Recommended Reading How to create a Histogram using LINQ

Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.