Load DataGridView on Scroll Down in C#

Introduction

This article provides an example of loading the data into a DataGridView on scroll down.

Step 1

First we create a table in a database as in the following:

  1. CREATE TABLE Table1  
  2. (  
  3.    id int identity(1,1),  
  4.    Column1 int,  
  5.    Column2 int  
  6. )  

Step 2

Now put data into the table (for a better understanding I filled in 1 to 60 numbers).

Step 3

Create a New Windows Form application. Drag and drop a DataGridView into a Windows Forms form.

Go to the form's properties then events then the Load event and then press the Enter key.

Property

Now go to the DataGridView's properties then events then scroll and then press the Enter key.

Now go to the DataGridView properties then events then SelectionChanged and then press the Enter key.

gridview

Step 4

Now paste in the code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10.   
  11. namespace LoadGridView  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         SqlConnection con = new SqlConnection("data source=Rahul-pc\\rahul_pc;initial catalog=example;integrated security=true;");  
  16.         DataTable dt = new DataTable();  
  17.         DataSet ds;  
  18.         BindingSource bi = new BindingSource();  
  19.         SqlDataAdapter da;  
  20.         int pageIndex = 1;  
  21.         int PageSize = 20;  
  22.   
  23.         public Form1()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         private void Form1_Load(object sender, EventArgs e)  
  29.         {  
  30.             LoadGridView();  
  31.         }  
  32.   
  33.         private void LoadGridView()  
  34.         {  
  35.             string str = "select * from Table1 where id between "+pageIndex+" and "+PageSize;  
  36.             da = new SqlDataAdapter(str,con);  
  37.             ds = new DataSet();  
  38.             da.Fill(ds);  
  39.             dt = ds.Tables[0];  
  40.             bi.DataSource = dt;  
  41.             dataGridView1.DataSource = bi;  
  42.             dataGridView1.ClearSelection();  
  43.         }  
  44.   
  45.         private int GetDisplayedRowsCount()  
  46.         {  
  47.             int count = dataGridView1.Rows[dataGridView1.FirstDisplayedScrollingRowIndex].Height;  
  48.             count = dataGridView1.Height / count;  
  49.             return count;  
  50.         }  
  51.   
  52.         private void dataGridView1_Scroll(object sender, ScrollEventArgs e)  
  53.         {  
  54.             int display = dataGridView1.Rows.Count - dataGridView1.DisplayedRowCount(false);  
  55.             if (e.Type == ScrollEventType.SmallIncrement || e.Type == ScrollEventType.LargeIncrement)  
  56.             {  
  57.                 if (e.NewValue >= dataGridView1.Rows.Count - GetDisplayedRowsCount())  
  58.                 {  
  59.                     string str = "select * from Table1 where id between " + ((pageIndex * PageSize) + 1) + " and " + ((pageIndex + 1) * PageSize);  
  60.                     da = new SqlDataAdapter(str, con);  
  61.                     ds = new DataSet();  
  62.                     da.Fill(ds);  
  63.                     dt.Merge(ds.Tables[0]);  
  64.                     bi.DataSource = dt;  
  65.                     dataGridView1.ClearSelection();  
  66.                     dataGridView1.FirstDisplayedScrollingRowIndex = display;  
  67.                     pageIndex++;  
  68.                 }  
  69.             }  
  70.         }  
  71.   
  72.         private void dataGridView1_SelectionChanged(object sender, EventArgs e)  
  73.         {  
  74.             dataGridView1.ClearSelection();  
  75.         }       
  76.     }  
  77. }  
Now run the project and see that when you scroll down, data will be loaded.

run the project

id

table

I hope you understand how to add data from a database into a DataGridView on scroll down.

Up Next
    Ebook Download
    View all
    Learn
    View all