Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Paging Class for .NET

0.00/5 (No votes)
17 Sep 2007 1  
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