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
1 class XofYPager : ITemplate
2 {
3 GridView _grid;
4 String _format;
5 public XofYPager(string format, GridView grid)
6 {
7 _format = format;
8 _grid = grid;
9
10 }
11 public void InstantiateIn(Control container)
12 {
13 Table tbl = new Table();
14 container.Controls.Add(tbl);
15 tbl.Width = Unit.Percentage(100);
16 TableRow row = new TableRow();
17 tbl.Rows.Add(row);
18 TableCell cell = new TableCell();
19 row.Cells.Add(cell);
20 cell.HorizontalAlign = HorizontalAlign.Center;
21
22 int currentPage = _grid.PageIndex + 1;
23 if (currentPage > 1)
24 {
25 ImageButton prevBtn = new ImageButton();
26 prevBtn.ImageUrl = "~/_layouts/images/prev.gif";
27 prevBtn.CommandName = "Page";
28 prevBtn.CommandArgument = "Prev";
29 cell.Controls.Add(prevBtn);
30 }
31 cell.Controls.Add(new LiteralControl(String.Format(_format, currentPage, _grid.PageCount)));
32 if (currentPage < _grid.PageCount)
33 {
34 ImageButton nextBtn = new ImageButton();
35 nextBtn.ImageUrl = "~/_layouts/images/next.gif";
36 nextBtn.CommandName = "Page";
37 nextBtn.CommandArgument = "Next";
38 cell.Controls.Add(nextBtn);
39 }
40 }
41 }
1 class SmartPager : ITemplate
2 {
3 GridView _grid;
4 int _additionalpages;
5 public SmartPager(int additionalpages, GridView grid)
6 {
7 _additionalpages = additionalpages;
8 _grid = grid;
9
10 }
11 public void InstantiateIn(Control container)
12 {
13 Table tbl = new Table();
14 container.Controls.Add(tbl);
15 tbl.Width = Unit.Percentage(100);
16 TableRow row = new TableRow();
17 tbl.Rows.Add(row);
18 TableCell cell = new TableCell();
19 row.Cells.Add(cell);
20 cell.HorizontalAlign = HorizontalAlign.Center;
21
22 int currentPage = _grid.PageIndex + 1;
23 if (_grid.PageCount < (5+_additionalpages*2)+1)
24 {
25 AddPages(cell,1,_grid.PageCount,currentPage);
26 }
27 else
28 {
29 if (currentPage < 4+_additionalpages)
30 {
31 AddPages(cell,1,3+_additionalpages*2,currentPage);
32 cell.Controls.Add(new LiteralControl("..."));
33 AddPages(cell,_grid.PageCount,_grid.PageCount,currentPage);
34 }
35 else
36 {
37 AddPages(cell,1,1,currentPage);
38 cell.Controls.Add(new LiteralControl("..."));
39 if (currentPage>_grid.PageCount-3-_additionalpages)
40 AddPages(cell,_grid.PageCount-2-_additionalpages*2,_grid.PageCount,currentPage);
41 else
42 {
43 AddPages(cell,currentPage-_additionalpages,currentPage+_additionalpages,currentPage);
44 cell.Controls.Add(new LiteralControl("..."));
45 AddPages(cell,_grid.PageCount,_grid.PageCount,currentPage);
46 }
47 }
48 }
49 }
50
51 void AddPages(Control container, int from, int to, int current)
52 {
53 for (int i = from; i <= to; i++)
54 {
55 LinkButton link=new LinkButton();
56 link.CommandName="Page";
57 link.CommandArgument=i.ToString();
58 link.Text = i.ToString();
59 if (i == current)
60 link.Style.Add(HtmlTextWriterStyle.FontWeight,"700");
61 container.Controls.Add(link);
62 if (i<to)
63 container.Controls.Add(new LiteralControl("|"));
64 }
65 }
66 }