Skip to main content

Custom pagers for SPGridView

I don’t think that the built in pagers for SPGridView are the most useful and informative, so for one of my current projects I decided to implement two custom pagers.

The first was XofYPager which shows the page numbers like this: 1 of 24 7 of 24 24 of 24

The second was SmartPager which shows the page numbers like this: 1 of 24 7 of 24 24 of 24

The use of these pagers are very simple just include the classes below and then before you DataBind() your SPGridView set the PagerTemplate property to an instance of one of these classes.

The XofYPager’s constructor should have two parameters:

class XofYPager : ITemplate
{
    GridView _grid;
    String _format;
    public XofYPager(string format, GridView grid)
    {
        _format = format;
        _grid = grid;
    }

    public void InstantiateIn(Control container)
    {
        Table tbl = new Table();
        container.Controls.Add(tbl);
        tbl.Width = Unit.Percentage(100);
        TableRow row = new TableRow();
        tbl.Rows.Add(row);
        TableCell cell = new TableCell();
        row.Cells.Add(cell);
        cell.HorizontalAlign = HorizontalAlign.Center;

        int currentPage = _grid.PageIndex + 1;
        if (currentPage > 1)
        {
            ImageButton prevBtn = new ImageButton();
            prevBtn.ImageUrl = "~/_layouts/images/prev.gif";
            prevBtn.CommandName = "Page";
            prevBtn.CommandArgument = "Prev";
            cell.Controls.Add(prevBtn);
        }
        cell.Controls.Add(new LiteralControl(String.Format(_format, currentPage, _grid.PageCount)));
        if (currentPage < _grid.PageCount)
        {
            ImageButton nextBtn = new ImageButton();
            nextBtn.ImageUrl = "~/_layouts/images/next.gif";
            nextBtn.CommandName = "Page";
            nextBtn.CommandArgument = "Next";
            cell.Controls.Add(nextBtn);
        }
    }
}

The SmartPager’s constructor should have two parameters:

class SmartPager : ITemplate
{
    GridView _grid;
    int _additionalpages;

    public SmartPager(int additionalpages, GridView grid)
    {
        _additionalpages = additionalpages;
        _grid = grid;
    }

    public void InstantiateIn(Control container)
    {
        Table tbl = new Table();
        container.Controls.Add(tbl);
        tbl.Width = Unit.Percentage(100);
        TableRow row = new TableRow();
        tbl.Rows.Add(row);
        TableCell cell = new TableCell();
        row.Cells.Add(cell);
        cell.HorizontalAlign = HorizontalAlign.Center;

        int currentPage = _grid.PageIndex + 1;
        if (_grid.PageCount < (5+_additionalpages*2)+1)
        {
            AddPages(cell,1,_grid.PageCount,currentPage);
        }
        else
        {
            if (currentPage < 4+_additionalpages)
            {
                AddPages(cell,1,3+_additionalpages*2,currentPage);
                cell.Controls.Add(new LiteralControl("..."));
                AddPages(cell,_grid.PageCount,_grid.PageCount,currentPage);
            }
            else
            {
                AddPages(cell,1,1,currentPage);
                cell.Controls.Add(new LiteralControl("..."));
                if (currentPage>_grid.PageCount-3-_additionalpages)
                    AddPages(cell,_grid.PageCount-2-_additionalpages*2,_grid.PageCount,currentPage);
                else
                {
                    AddPages(cell,currentPage-_additionalpages,currentPage+_additionalpages,currentPage);
                    cell.Controls.Add(new LiteralControl("..."));
                    AddPages(cell,_grid.PageCount,_grid.PageCount,currentPage);
                }
            }
        }
    }

    void AddPages(Control container, int from, int to, int current)
    {
        for (int i = from; i <= to; i++)
        {
            LinkButton link=new LinkButton();
            link.CommandName="Page";
            link.CommandArgument=i.ToString();
            link.Text = i.ToString();
            if (i == current)
                link.Style.Add(HtmlTextWriterStyle.FontWeight,"700");
            container.Controls.Add(link);
            if (i<to)
                container.Controls.Add(new LiteralControl("|"));
        }
    }
}

Copyright © 2007-2022