Displaying Paging In ASP.Net

            GridViewRow pager = SearchResultsGridView.TopPagerRow;

            if (pager != null)
            {
                int pageCount = PageCount; // SearchResultsGridView.PageCount;
                int pageSize = SearchResultsGridView.PageSize;
                int columnCount = 10;

                if (pageCount > 1)
                {
                    Label pagerLabel = (Label)pager.Cells[0].FindControl("PagerLabel");
                    pagerLabel.Text = pagerLabel.Text.Replace("<transactions>", ResultCount.ToString());
                    pagerLabel.Text = pagerLabel.Text.Replace("<start>", (((CurrentPage) * SearchResultsGridView.PageSize) + 1).ToString());
                    pagerLabel.Text = pagerLabel.Text.Replace("<end>", (((CurrentPage + 1) * SearchResultsGridView.PageSize)).ToString());
                    PlaceHolder pagerPlaceHolder = (PlaceHolder)pager.Cells[0].FindControl("PlaceHolder1");
                    bool breakLoop = false;

                    if (pagerPlaceHolder != null)
                    {
                        Table myTable = new Table();
                        int rowCount = (pageCount / columnCount) + (((pageCount % columnCount) == 0) ? 0 : 1);

                        for (int row = 0; row < rowCount; row++)
                        {
                            int firstPageInRow = row * columnCount;
                            int lastPageInRow = ((firstPageInRow + columnCount - 1) < pageCount) ?
                                                 (firstPageInRow + columnCount - 1) : pageCount - 1;
                            TableRow myRow = new TableRow();

                            for (int column = 0; column < columnCount; column++)
                            {
                                // build the row out of the pageCount for the column
                                TableCell myColumn = new TableCell();
                                int startingRecord = (row * columnCount * pageSize) + (column * pageSize) + 1;
                                int endingRecord = (row * columnCount * pageSize) + (column * pageSize) + pageSize;

                                if (endingRecord > ResultCount)
                                {
                                    endingRecord = ResultCount;
                                    breakLoop = true;
                                }

                                string pageLabel = startingRecord + " - " + endingRecord;
                                int page = (row * columnCount) + column;

                                if (page == CurrentPage)
                                {
                                    // display a Label instead of a LinkButton
                                    Label label = new Label();
                                    label.Text = pageLabel;
                                    //label.ID = "Page" + (page + 1).ToString();
                                    myColumn.Controls.Add(label);
                                }
                                else
                                {
                                    // diplay a LinkButton for the page
                                    LinkButton button = new LinkButton();
                                    button.Text = pageLabel;
                                    button.CommandName = "Page";
                                    button.CommandArgument = (page + 1).ToString();
                                    //button.ID = "Page" + (page + 1).ToString();
                                    myColumn.Controls.Add(button);
                                }

                                myRow.Cells.Add(myColumn);

                                if (breakLoop)
                                {
                                    break;
                                }
                            }

                            myTable.Rows.Add(myRow);

                            if (breakLoop)
                            {
                                break;
                            }
                        }

                        pagerPlaceHolder.Controls.Add(myTable);
                    }

                    pager.Visible = true;
                }
                else
                {
                    pager.Visible = false;
                }
            }

Advertisements