Introduction

  • Here, I will explain how to use custom pagination in GridView with formatting, using ASP.NET.
  • Follow the steps mentioned below to achieve our requirement.
Step 1

Follow the code, mentioned below, to achieve the result Default.aspx.
  1. <asp:GridView ID="CustomGrid" runat="server" EnableModelValidation="True" AutoGenerateColumns="False" AllowPaging="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4″ ForeColor=" Black " GridLines="Vertical "
  2. EmptyDataText="No record found. " PageSize="2″>
  3. <AlternatingRowStyle BackColor="White" />
  4. <Columns>
  5. <asp:TemplateField HeaderText="UserID">
  6. <ItemTemplate>
  7. <asp:Label ID="lblUser" runat="server" Text=’<%# Eval( "ID") %>’></asp:Label>
  8. </ItemTemplate>
  9. </asp:TemplateField>
  10. <asp:BoundField DataField="NAME" HeaderText="Name">
  11. </asp:BoundField>
  12. <asp:BoundField DataField="PRICE" HeaderText="Price">
  13. </asp:BoundField>
  14. </Columns>
  15. <FooterStyle BackColor="#CCCC99″ />
  16. <HeaderStyle BackColor=" #6B696B " Font-Bold="True " ForeColor="White " />
  17. <PagerSettings FirstPageText="First " LastPageText="Last " NextPageText="Next " Position="TopAndBottom "
  18. PreviousPageText="Prev " />
  19. <PagerStyle BackColor="#F7F7DE " ForeColor="Black " HorizontalAlign="Right " />
  20. <PagerTemplate>
  21. <div>
  22. <span style="float: right; ">
  23. <asp:ImageButton ID="ImageButtonNext " runat="server " ImageUrl="~/images/r-arrow.png "
  24. OnClick="ImageButtonNext_Click " />
  25. </span><span style="float: right; padding-left: 5px; padding-right: 5px; ">
  26. <asp:Label ID="LabelCurrentIndex " runat="server " Text=’<%= CurrentSelectedIndex %>’></asp:Label>
  27. Of
  28. <asp:Label ID="LabelLastIndex " runat="server " Text=’<%= TotalPageCount %>’></asp:Label>
  29. </span><span style="float: right; ">
  30. <asp:ImageButton ID="ImageButtonPrev " runat="server " ImageUrl="~/images/l-arrow.png "
  31. OnClick="ImageButtonPrev_Click " />
  32. </span>
  33. </div>
  34. </PagerTemplate>
  35. <RowStyle BackColor="#F7F7DE " />
  36. <SelectedRowStyle BackColor="#CE5D5A " Font-Bold="True " ForeColor="White " />
  37. </asp:GridView>
  38. <input type="hidden " id="GridActiveIndex " value="0″ runat="server" />
Step 2

Add the code, mentioned below, to Default.aspx.cs.
  1. public string currentSelectedIndex = string.Empty;
  2. public string totalPageCount = string.Empty;
  3. public string UserEmail = string.Empty;
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6. if (!IsPostBack)
  7. {
  8. LoadData();
  9. int currentIndex = Convert.ToInt32(Convert.ToString(GridActiveIndex.Value));
  10. if (currentIndex > 0)
  11. {
  12. CustomGrid.PageIndex = currentIndex;
  13. }
  14. }
  15. }
  16. private void LoadData()
  17. {
  18. DataTable dt = new DataTable();
  19. dt = getResultTable();
  20. CustomGrid.DataSource = dt;
  21. CustomGrid.DataBind();
  22. GridActiveIndex.Value = '0″;
  23. FotmatPager(0);
  24. }
  25. DataTable getResultTable()
  26. {
  27. DataTable dtproduct = new DataTable();
  28. dtproduct.Columns.Add('ID', typeof(int));
  29. dtproduct.Columns.Add('NAME', typeof(string));
  30. dtproduct.Columns.Add('PRICE', typeof(int));
  31. // Here we add five DataRows.
  32. dtproduct.Rows.Add(1, 'Product1″, 100);
  33. dtproduct.Rows.Add(2, 'Product2″, 110);
  34. dtproduct.Rows.Add(3, 'Product3″, 120);
  35. dtproduct.Rows.Add(4, 'Product4″, 130);
  36. dtproduct.Rows.Add(5, 'Product5″, 140);
  37. return dtproduct;
  38. }
  39. #region GridVariable
  40. GridViewRow pagerRow;
  41. int current_index;
  42. int page_count;
  43. #endregion
  44. public string CurrentSelectedIndex
  45. {
  46. get { return currentSelectedIndex; }
  47. set { currentSelectedIndex = value; }
  48. }
  49. public string TotalPageCount
  50. {
  51. get { return totalPageCount; }
  52. set { totalPageCount = value; }
  53. }
  54. protected void formatTopPager(int current, int total)
  55. {
  56. try
  57. {
  58. pagerRow = CustomGrid.TopPagerRow;
  59. Label lblcurrentindex =
  60. (Label)pagerRow.Cells[0].FindControl('LabelCurrentIndex');
  61. lblcurrentindex.Text = current.ToString();
  62. Label lbllastindex =
  63. (Label)pagerRow.Cells[0].FindControl('LabelLastIndex');
  64. lbllastindex.Text = total.ToString();
  65. CurrentSelectedIndex = current.ToString();
  66. TotalPageCount = total.ToString();
  67. }
  68. catch (Exception)
  69. {
  70. //throw;
  71. }
  72. }
  73. protected void fotmatBottomPager(int current, int total)
  74. {
  75. try
  76. {
  77. pagerRow = CustomGrid.BottomPagerRow;
  78. Label lblcurrentindex =
  79. (Label)pagerRow.Cells[0].FindControl('LabelCurrentIndex');
  80. lblcurrentindex.Text = current.ToString();
  81. Label lbllastindex =
  82. (Label)pagerRow.Cells[0].FindControl('LabelLastIndex');
  83. lbllastindex.Text = total.ToString();
  84. CurrentSelectedIndex = current.ToString();
  85. TotalPageCount = total.ToString();
  86. }
  87. catch (Exception)
  88. {
  89. // throw;
  90. }
  91. }
  92. protected void FotmatPager(int current)
  93. {
  94. ViewState["currentPageIndex"] = current;
  95. current += 1;
  96. int total = CustomGrid.PageCount;
  97. formatTopPager(current, total);
  98. fotmatBottomPager(current, total);
  99. CustomGrid.PageIndex = Convert.ToInt32(Convert.ToString(ViewState["currentPageIndex"]));
  100. }
  101. protected void ManageGridLayout(DataTable dt, int selected_pageIndex, int totalPage)
  102. {
  103. CustomGrid.PageIndex = selected_pageIndex;
  104. CustomGrid.DataSource = getResultTable();
  105. CustomGrid.DataBind();
  106. FotmatPager(selected_pageIndex);
  107. }
  108. protected void ImageButtonNext_Click(object sender, ImageClickEventArgs e)
  109. {
  110. current_index = CustomGrid.PageIndex;
  111. page_count = CustomGrid.PageCount;
  112. if (current_index + 1 < page_count)
  113. ManageGridLayout(getResultTable(), current_index + 1, page_count);
  114. }
  115. protected void ImageButtonPrev_Click(object sender, ImageClickEventArgs e)
  116. {
  117. current_index = CustomGrid.PageIndex;
  118. page_count = CustomGrid.PageCount;
  119. if (current_index > 0)
  120. ManageGridLayout(getResultTable(), current_index – 1, page_count);
  121. }
Output