Click here to Skip to main content
11,805,316 members (65,433 online)
Click here to Skip to main content

A Simple Way for Paging in DataGridView in WinForm Applications

, 15 Jun 2011 CPOL 78K 5.3K 40
Rate this:
Please Sign up or sign in to vote.
An easy way for paging DataGridView


I have recently worked on paging in DataGridView for a WinForm application. I Googled it, but generally found very complicated codes. So I wanted to write my code to be easily understood by everyone. After some modifications, I tried to make my code really simple and want to share it with others.


I have used button controls for the purpose of navigating switches.

I added required variables such as PgSize for number of rows per page, CurrentPageIndex for current page index and Totalpage for calculating maximum number of pages DataGridView is able to display.

private int PgSize = 20; 
private int CurrentPageIndex = 1;
private int TotalPage=0; 

As the name suggests, the following method is used to calculate the total number of pages for gridview:

private void CalculateTotalPages()
    int rowCount = ds.Tables["Customers"].Rows.Count;
    TotalPage = rowCount / PgSize;
    // if any row left after calculated pages, add one more page 
    if (rowCount % PgSize > 0)
        TotalPage += 1;

In order to retrieve records to display for the navigated page, I used a DataTable returned from GetCurrentRecord() method as datasource for gridview. This method returns the top 20 rows, (Size of PgSize variable) if navigated page is one. If navigated page is greater than one, then it calculates the previous pageOffset through the following line of code:

int PreviousPageOffSet= (page - 1) * PgSize; 

Now the query work starts, to select only pages from the current page and leaving earlier records apply query available in the following code:

private DataTable GetCurrentRecords(int page, SqlConnection con)
    DataTable dt = new DataTable();

    if (page == 1)
        cmd2 = new SqlCommand("Select TOP " + PgSize + 
		" * from Customers ORDER BY CustomerID", con);
        int PreviousPageOffSet= (page - 1) * PgSize;

        cmd2 = new SqlCommand("Select TOP " + PgSize +
        	" * from Customers WHERE CustomerID NOT IN " +
        	"(Select TOP " + PreviousPageOffSet+ 
	" CustomerID from customers ORDER BY CustomerID) ", con);
        // con.Open();
        this.adp1.SelectCommand = cmd2;
    return dt;

Navigating buttons are as usual. Here we only calculate the navigating pageIndex and pass it to GetCurrentRecords() method as argument to retrieve dataSource for our datagridview.

    private void btnFirstPage_Click(object sender, EventArgs e)
        this.CurrentPageIndex = 1;
        this.dataGridView1.DataSource = GetCurrentRecords(this.CurrentPageIndex, con);

    private void btnNxtPage_Click(object sender, EventArgs e)
        if (this.CurrentPageIndex < this.TotalPage)
            this.dataGridView1.DataSource = 
		GetCurrentRecords(this.CurrentPageIndex, con);

    private void btnPrevPage_Click(object sender, EventArgs e)
        if (this.CurrentPageIndex > 1)
            this.dataGridView1.DataSource = 
		GetCurrentRecords(this.CurrentPageIndex, con);

    private void btnLastPage_Click(object sender, EventArgs e)
        this.CurrentPageIndex = TotalPage;
        this.dataGridView1.DataSource = GetCurrentRecords(this.CurrentPageIndex, con); 

    private void button1_Click(object sender, EventArgs e)

I hope this is the easiest way for paging a DataGridView.

This is my first article I have ever posted and I have tried my best to explain my code. So I welcome any type of suggestions.


  • 15th June, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

sk saini
Software Developer (Senior) Embossoft Solutions
India India
Software developer with 3+ years of experience in c# Winform applications development on ASP.NET platform.

You may also be interested in...

Comments and Discussions

SuggestionThank you Pin
An@nd Rajan19-Jan-14 21:54
professionalAn@nd Rajan19-Jan-14 21:54 
Questionsearch datagridview with paging c# Pin
Member 104267332-Dec-13 20:41
memberMember 104267332-Dec-13 20:41 
QuestionPaging bottom Pin
Member 1041171221-Nov-13 3:14
memberMember 1041171221-Nov-13 3:14 
SuggestionPaging code on DatagridView Pin
Member 1028363216-Oct-13 20:51
memberMember 1028363216-Oct-13 20:51 
QuestionGetting Exception Pin
Member 101748035-Aug-13 19:54
memberMember 101748035-Aug-13 19:54 
Questionhi everybody if any of you are still interested in datagridview paging, would you please try this one? thanks! Pin
Alejandro Miralles28-Dec-12 8:35
memberAlejandro Miralles28-Dec-12 8:35 
QuestionThank's Buddy Pin
deid4r419-Dec-12 16:16
memberdeid4r419-Dec-12 16:16 
GeneralVirtual Mode is better Pin
namdia16-Jun-11 8:32
membernamdia16-Jun-11 8:32 
GeneralMy vote of 2 Pin
Selvin15-Jun-11 23:43
memberSelvin15-Jun-11 23:43 
GeneralMy vote of 5 Pin
Monjurul Habib15-Jun-11 8:21
memberMonjurul Habib15-Jun-11 8:21 
GeneralMy vote of 4 Pin
rajeshrisharma15-Jun-11 4:31
memberrajeshrisharma15-Jun-11 4:31 
GeneralRe: My vote of 4 Pin
madanbhanu9-Aug-12 19:26
membermadanbhanu9-Aug-12 19:26 
GeneralRe: My vote of 4 Pin
Dirk_Strauss12-Feb-14 12:46
memberDirk_Strauss12-Feb-14 12:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 15 Jun 2011
Article Copyright 2011 by sk saini
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid