Click here to Skip to main content
Licence 
First Posted 17 Sep 2007
Views 12,916
Downloads 127
Bookmarked 17 times

Paging Class for .NET

By | 17 Sep 2007 | Article
a customizable class that can be used for paging

Introduction

I have explained a way to achieve custom paging a previous article. Using the same logic I have created a custom class that can be used for custom paging very easily same as the PagedDataSource class in .NET 2.0 .

Here is the class SPagingSource:

{
#region Private Members
    private DataTable _dtSource = new DataTable();
    private DataTable _dtDataSource = new DataTable();
    private int _pageIndex = 1;
    private int _pageSize = 20;
    private enum PagingMode { First = 1, Next = 2, Previous = 3, Last = 4 };
#endregion

#region Properties
    public DataTable SourceData
    {
        set
        {
            _dtSource = value;
        }
    }

    public DataView DataSource
    {
        get
        {
            if (_dtDataSource.Rows.Count <= 0)
            {
                CustomPaging((int)PagingMode.First);
            }
            return _dtDataSource.DefaultView;
        }
    }

    public int PageIndex
    {
        get
        {
            return _pageIndex;
        }
    }

    public int MoveTo
    {
        set
        {
            if (_pageIndex == value)
            {
                _pageIndex = value;
            }
            else if (_pageIndex < value)
            {
                _pageIndex = value-1;
                CustomPaging((int)PagingMode.Next); 
            }
            else if (_pageIndex > value)
            {
                _pageIndex = value + 1;
                CustomPaging((int)PagingMode.Previous); 
            }
        }
    }

    public int PageSize
    {
        get
        {
            return _pageSize;
        }
        set
        {
            _pageSize = value;
        }
    }

    public int PageNumber
    {
        get
        {
            return _pageIndex;
        }
    }

    public bool IsFirstPage
    {
        get
        {
            if (_pageIndex == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public bool IsLastPage
    {
        get
        {
            if (_pageIndex == (_dtSource.Rows.Count/_pageSize))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

#endregion

#region Private Functions

    public void Next()
    {
        CustomPaging((int)PagingMode.Next);
    }

    public void Previous()
    {
        CustomPaging((int)PagingMode.Previous);
    }

    public void First()
    {
        CustomPaging((int)PagingMode.First);
    }

    public void Last()
    {
        CustomPaging((int)PagingMode.Last);
    }

    private void CustomPaging(int mode)
    {
        if (_dtSource.Rows.Count <= _pageSize)
        {
            return;
        }

        switch (mode)
        {
        case (int)PagingMode.Next:
            if (_dtSource.Rows.Count > (_pageIndex * _pageSize))
            {
                DataTable tmpTable = new DataTable();
                tmpTable = _dtSource.Clone();

                if (_dtSource.Rows.Count >= ((_pageIndex * _pageSize) + 
                    _pageSize))
                {
                    for (int i = _pageIndex * _pageSize; i < ((
                        _pageIndex * _pageSize) + _pageSize); i++)
                    {
                        tmpTable.ImportRow(_dtSource.Rows[i]);
                    }
                }
                else
                {
                    for (int i = _pageIndex * _pageSize; i < 
                        _dtSource.Rows.Count; i++)
                    {
                        tmpTable.ImportRow(_dtSource.Rows[i]);
                    }
                }

                _pageIndex += 1;

                _dtDataSource = tmpTable;
                tmpTable.Dispose();
            }
            break;
        case (int)PagingMode.Previous:
            if (_pageIndex > 1)
            {
                DataTable tmpTable = new DataTable();
                tmpTable = _dtSource.Clone();

                _pageIndex -= 1;

                for (int i = ((_pageIndex * _pageSize) - _pageSize); i < (
                    _pageIndex * _pageSize); i++)
                {
                    tmpTable.ImportRow(_dtSource.Rows[i]);
                }
                _dtDataSource = tmpTable;
                tmpTable.Dispose();
            }
            break;
        case (int)PagingMode.First:
            _pageIndex = 2;
            CustomPaging((int)PagingMode.Previous);
            break;
        case (int)PagingMode.Last:
            _pageIndex = (_dtSource.Rows.Count / _pageSize);
            CustomPaging((int)PagingMode.Next);
            break;
        }
    }
#endregion
}

Just copy paste or download the attched source code. If you copy/paste create a new class in your project. Copy this code to that class except the class definitions. If you download, add this class to the project. Add your namespace. Change the class name if you want.

Now we have the paging class. I will show how to use this class. First create a object for the class with page level scope,

SPagingSource sPS = new SPagingSource();

In the bind data section:

ds.Tables[0] = //Datasource

//Assigning the datasource to the 'sPS' object.
sPS.SourceData = ds.Tables[0];

//Setting the page size.
sPS.PageSize = 10;

//Assign the page class object as the datasource.
ListView1.DataContext = sPS.DataSource; (WPF)

        OR

DataView1.DataSource = sPS.DataSource; (.Net2.0)

Now we have the grid/List with first 10 (here I set the pagesize as 10) set of records. For navigation we just need to call the Next, Previous, Last and First methods of the paging class object and reassign the datasource. For example, for displaying next set of records,

sPS.Next();
ListView1.DataContext = sPS.DataSource;

You can also use the MoveTo property of the class object to move to a page.

sPS.MoveTo = 3;
ListView1.DataContext = sPS.DataSource;

There is also some useful properties available like to get the current page index.

Label1.Text = sPS.PageNumber;

To check whether we are at Lastpage/Firstpage we have

sPS.IsLastPage
sPS.IsFirstPage     boolean properties.

After all you can modify the class to suit your needs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sreejith Thathanattu

Software Developer

India India

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
GeneralPagesize PinmemberMember 26226230:51 8 Dec '07  
GeneralUsing enums... PinmemberHotcut0:35 17 Sep '07  
GeneralRe: Using enums... PinmemberSreejith Thathanattu0:45 18 Sep '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 17 Sep 2007
Article Copyright 2007 by Sreejith Thathanattu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid