Click here to Skip to main content
Licence CPOL
First Posted 20 Jun 2007
Views 32,823
Downloads 225
Bookmarked 17 times

An extended version of DataGridView with paging

By | 20 Jun 2007 | Article
This does what it says on the tin. I was inspired to extend the DataGridView control after reading a similar article by D Strauss.

Screenshot - PagingDataGridView_scr.jpg

Introduction

This is my first CodeProject article. This control is a simple extension of the DataGridView control that ships with Visual Studio .NET. Before anyone comes back at me with comments about optimization of code etc., I may well update it in the future.

Background

After reading D Straus' article about making a DataGridView page, I thought I'd extend it to include paging.

Using the code

The use of this control is pretty straightforward. Download the source, compile the DLL, import it into your Toolbox in Designer mode in VS, and away you go.

The main methods are:

  • GoToNextPage()
  • oToLastPage()
  • GoToPreviousPage()
  • GoToFirstPage()
  • GoToPageNumber(int pageno)
public void GoToPageNumber(int n)
{
    DataSet dsTempSubSet = new DataSet();
    DataRow[] rows = new DataRow[RowsPerPage];
    
    if (n == 1)
    {
        GoToFirstPage();
    }
    if ((0 < n) && (n <= GetTotalPages()))
    {
        int PageIndex = (n - 1) * RowsPerPage;
        if (PageIndex >= dsTemp.Tables[0].Rows.Count)
        {
            GoToFirstPage();
        }
        else
        {
            int WholePages = dsTemp.Tables[0].Rows.Count / RowsPerPage;
            if ((dsTemp.Tables[0].Rows.Count % RowsPerPage) != 0 && n == GetTotalPages())
            {
                rows = new DataRow[dsTemp.Tables[0].Rows.Count - 
                                       (WholePages * RowsPerPage)];
            }
            for (int i = 0, i2 = PageIndex; i < rows.Length && i2 < 
                                 dsTemp.Tables[0].Rows.Count; i2++, i++)
            {
                rows[i] = dsTemp.Tables[0].Rows[i2];
            }
            dsTempSubSet.Merge(rows);
            bindingSource1.DataSource = dsTempSubSet.Tables[0];
            this.DataSource = bindingSource1;
            CurrentPage = n;
        }
    }
}

History

  • 1.1 - 21-June-2007: Optimized code, most nav methods now use GoToPageNumber(int).
  • 1.0 - 20-June-2007: Initial upload.

License

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

About the Author

berghain

Software Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalproblems in updating Pinmemberanat24031:53 5 Aug '07  
Generaladd row number to this pagingdatagridview Pinmemberanat24039:16 28 Jul '07  
GeneralRe: add row number to this pagingdatagridview Pinmemberanat240322:43 29 Jul '07  
GeneralRe: add row number to this pagingdatagridview Pinmembertopherino0:21 30 Jul '07  
GeneralProblem in sort and fixing the problem Pinmemberanat24036:42 27 Jul '07  
Generalsort option Pinmemberanat24032:25 25 Jul '07  
GeneralRe: sort option Pinmemberanat24031:05 26 Jul '07  
QuestionHow the functions work? Pinmemberanat24038:43 7 Jul '07  
AnswerRe: How the functions work? Pinmembertopherino8:52 7 Jul '07  
GeneralRe: How the functions work? Pinmemberanat24039:46 7 Jul '07  
GeneralRe: How the functions work? Pinmembertopherino10:33 7 Jul '07  
GeneralRe: How the functions work? Pinmemberanat240311:10 7 Jul '07  
Questionhow to import it into your toolbox in Designer mode Pinmemberanat24037:09 7 Jul '07  
AnswerRe: how to import it into your toolbox in Designer mode Pinmembertopherino7:39 7 Jul '07  
GeneralGood but... PinmemberD Strauss8:01 20 Jun '07  
GeneralRe: Good but... Pinmembertopherino22:27 20 Jun '07  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 20 Jun 2007
Article Copyright 2007 by berghain
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid