1: protected void managePaging(DataTable _dt)
2: {
3: if (_dt.Rows.Count > 0)
4: {
5:
6: // Variable declaration
7: int numberOfPages;
8: int numberOfRecords = dt.Rows.Count;
9: int currentPage = (GridView1.PageIndex);
10: StringBuilder strSummary = new StringBuilder();
11:
12:
13: // If number of records is more then the page size (specified in global variable)
14: // Just to check either gridview have enough records to implement paging
15: if (numberOfRecords > pageSize)
16: {
17: // Calculating the total number of pages
18: numberOfPages = (int)Math.Ceiling((double)numberOfRecords / (double)pageSize);
19: }
20: else
21: {
22: numberOfPages = 1;
23: }
24:
25:
26: // Creating a small summary for records.
27: strSummary.Append("Displaying <b>");
28:
29: // Creating X f X Records
30: int floor = (currentPage * pageSize) + 1;
31: strSummary.Append(floor.ToString());
32: strSummary.Append("</b>-<b>");
33: int ceil = ((currentPage * pageSize) + pageSize);
34:
35: //let say you have 26 records and you specified 10 page size,
36: // On the third page it will return 30 instead of 25 as that is based on pageSize
37: // So this check will see if the ceil value is increasing the number of records. Consider numberOfRecords
38: if (ceil > numberOfRecords)
39: {
40: strSummary.Append(numberOfRecords.ToString());
41: }
42: else
43: {
44: strSummary.Append(ceil.ToString());
45: }
46:
47: // Displaying Total number of records Creating X of X of About X records.
48: strSummary.Append("</b> of About <b>");
49: strSummary.Append(numberOfRecords.ToString());
50: strSummary.Append("</b>Records</br>");
51:
52:
53: litPagingSummary.Text = strSummary.ToString();
54:
55:
56: //Variable declaration
57: //these variables will used to calculate page number display
58: int pageShowLimitStart = 1;
59: int pageShowLimitEnd = 1;
60:
61:
62:
63: // Just to check, either there is enough pages to implement page number display logic.
64: if (pageDispCount > numberOfPages)
65: {
66: pageShowLimitEnd = numberOfPages; // Setting the end limit to the number of pages. Means show all page numbers
67: }
68: else
69: {
70: if (currentPage > 4) // If page index is more then 4 then need to less the page numbers from start and show more on end.
71: {
72: //Calculating end limit to show more page numbers
73: pageShowLimitEnd = currentPage + (int)(Math.Floor((decimal)pageDispCount / 2));
74: //Calculating Start limit to hide previous page numbers
75: pageShowLimitStart = currentPage - (int)(Math.Floor((decimal)pageDispCount / 2));
76: }
77: else
78: {
79: //Simply Displaying the 10 pages. no need to remove / add page numbers
80: pageShowLimitEnd = pageDispCount;
81: }
82: }
83:
84: // Since the pageDispCount can be changed and limit calculation can cause < 0 values
85: // Simply, set the limit start value to 1 if it is less
86: if (pageShowLimitStart < 1)
87: pageShowLimitStart = 1;
88:
89:
90: //Dynamic creation of link buttons
91:
92: // First Link button to display with paging
93: LinkButton objLbFirst = new LinkButton();
94: objLbFirst.Click += new EventHandler(objLb_Click);
95: objLbFirst.Text = "First";
96: objLbFirst.ID = "lb_FirstPage";
97: objLbFirst.CommandName = "pgChange";
98: objLbFirst.EnableViewState = true;
99: objLbFirst.CommandArgument = "1";
100:
101: //Previous Link button to display with paging
102: LinkButton objLbPrevious = new LinkButton();
103: objLbPrevious.Click += new EventHandler(objLb_Click);
104: objLbPrevious.Text = "Previous";
105: objLbPrevious.ID = "lb_PreviousPage";
106: objLbPrevious.CommandName = "pgChange";
107: objLbPrevious.EnableViewState = true;
108: objLbPrevious.CommandArgument = currentPage.ToString();
109:
110:
111: //of course if the page is the 1st page, then there is no need of First or Previous
112: if (currentPage == 0)
113: {
114: objLbFirst.Enabled = false;
115: objLbPrevious.Enabled = false;
116: }
117: else
118: {
119: objLbFirst.Enabled = true;
120: objLbPrevious.Enabled = true;
121: }
122:
123:
124: //Adding control in a place holder
125: plcPaging.Controls.Add(objLbFirst);
126: plcPaging.Controls.Add(new LiteralControl(" | ")); // Just to give some space
127: plcPaging.Controls.Add(objLbPrevious);
128: plcPaging.Controls.Add(new LiteralControl(" | "));
129:
130:
131: // Creatig page numbers based on the start and end limit variables.
132: for (int i = pageShowLimitStart; i <= pageShowLimitEnd; i++)
133: {
134: if ((Page.FindControl("lb_" + i.ToString()) == null) && i <= numberOfPages)
135: {
136: LinkButton objLb = new LinkButton();
137: objLb.Click += new EventHandler(objLb_Click);
138: objLb.Text = i.ToString();
139: objLb.ID = "lb_" + i.ToString();
140: objLb.CommandName = "pgChange";
141: objLb.EnableViewState = true;
142: objLb.CommandArgument = i.ToString();
143:
144: if ((currentPage + 1) == i)
145: {
146: objLb.Enabled = false;
147: }
148:
149:
150: plcPaging.Controls.Add(objLb);
151: plcPaging.Controls.Add(new LiteralControl(" | "));
152: }
153: }
154:
155: // Last Link button to display with paging
156: LinkButton objLbLast = new LinkButton();
157: objLbLast.Click += new EventHandler(objLb_Click);
158: objLbLast.Text = "Last";
159: objLbLast.ID = "lb_LastPage";
160: objLbLast.CommandName = "pgChange";
161: objLbLast.EnableViewState = true;
162: objLbLast.CommandArgument = numberOfPages.ToString();
163:
164: // Next Link button to display with paging
165: LinkButton objLbNext = new LinkButton();
166: objLbNext.Click += new EventHandler(objLb_Click);
167: objLbNext.Text = "Next";
168: objLbNext.ID = "lb_NextPage";
169: objLbNext.CommandName = "pgChange";
170: objLbNext.EnableViewState = true;
171: objLbNext.CommandArgument = (currentPage + 2).ToString();
172:
173: //of course if the page is the last page, then there is no need of last or next
174: if ((currentPage + 1) == numberOfPages)
175: {
176: objLbLast.Enabled = false;
177: objLbNext.Enabled = false;
178: }
179: else
180: {
181: objLbLast.Enabled = true;
182: objLbNext.Enabled = true;
183: }
184:
185:
186: // Adding Control to the place holder
187: plcPaging.Controls.Add(objLbNext);
188: plcPaging.Controls.Add(new LiteralControl(" | "));
189: plcPaging.Controls.Add(objLbLast);
190: plcPaging.Controls.Add(new LiteralControl(" | "));
191: }
192:
193: }