Click here to Skip to main content
15,904,153 members
Articles / Programming Languages / C#

An extended version of DataGridView with paging

Rate me:
Please Sign up or sign in to vote.
2.07/5 (6 votes)
20 Jun 2007CPOL 57.3K   576   21   16
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)
C#
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)


Written By
Software Developer (Senior) ----
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalproblems in updating Pin
anat24035-Aug-07 1:53
anat24035-Aug-07 1:53 
Generaladd row number to this pagingdatagridview Pin
anat240328-Jul-07 9:16
anat240328-Jul-07 9:16 
GeneralRe: add row number to this pagingdatagridview Pin
anat240329-Jul-07 22:43
anat240329-Jul-07 22:43 
GeneralRe: add row number to this pagingdatagridview Pin
berghain30-Jul-07 0:21
berghain30-Jul-07 0:21 
GeneralProblem in sort and fixing the problem Pin
anat240327-Jul-07 6:42
anat240327-Jul-07 6:42 
Generalsort option Pin
anat240325-Jul-07 2:25
anat240325-Jul-07 2:25 
GeneralRe: sort option Pin
anat240326-Jul-07 1:05
anat240326-Jul-07 1:05 
QuestionHow the functions work? Pin
anat24037-Jul-07 8:43
anat24037-Jul-07 8:43 
AnswerRe: How the functions work? Pin
berghain7-Jul-07 8:52
berghain7-Jul-07 8:52 
GeneralRe: How the functions work? Pin
anat24037-Jul-07 9:46
anat24037-Jul-07 9:46 
GeneralRe: How the functions work? Pin
berghain7-Jul-07 10:33
berghain7-Jul-07 10:33 
GeneralRe: How the functions work? Pin
anat24037-Jul-07 11:10
anat24037-Jul-07 11:10 
Questionhow to import it into your toolbox in Designer mode Pin
anat24037-Jul-07 7:09
anat24037-Jul-07 7:09 
AnswerRe: how to import it into your toolbox in Designer mode Pin
berghain7-Jul-07 7:39
berghain7-Jul-07 7:39 
GeneralGood but... Pin
Dirk_Strauss20-Jun-07 8:01
professionalDirk_Strauss20-Jun-07 8:01 
You instanciated a DataSet in this method called dsTempSubSet that has no purpose. It's not being used. (See code below from the sourcecode you provided for download) Other than that, it is exactly what I wanted to do in my next article. By the way, in the Background section of your article you refer to me as "D Straus's ". Confused | :confused: It should be "D Strauss'". Smile | :)

public void GoToPreviousPage()
{
DataSet dsTempSubSet = new DataSet();
DataRow[] rows = new DataRow[RowsPerPage];

if(CurrentPage != 1)
{
GoToPageNumber(CurrentPage - 1);
//CurrentPage = dsTemp.Tables[0].Rows.Count / RowsPerPage;
//CurrentPage++;
}
}
GeneralRe: Good but... Pin
berghain20-Jun-07 22:27
berghain20-Jun-07 22:27 

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

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