custom paging in a gridview

Bijub
Posted by Bijub under ASP.NET category on | Points: 40 | Views : 4445
step1: custom paging in a grid view is achived by using the stored procedure described below. This procedure will retrieve records based on the pageSize and pageIndex.
pageSize is the number of records to be displayed in a page and pageIndex is the current pagenumber.

alter procedure pro_RetrieveRecordsBasedOnPageSize
@PageSize int,
@PageIndex int
as
begin
WITH LogEntries AS (
SELECT p.* ,ROW_NUMBER() OVER (ORDER BY Id)
AS Row
FROM Person p )

SELECT *
FROM LogEntries
WHERE Row between (((@PageIndex - 1) * @PageSize) + 1) and @PageIndex * @PageSize
end

execute pro_RetrieveRecordsBasedOnPageSize 5, 1


step2: //---------- in aspx file-----------
here first,previous,next and last linkbuttons are placed in a table to navigate through the grid. here we are not using the event and propert of grid. AllowPaging property to false and donot use PageIndexChanging event.

<table border = "0" width = "700">
<tr>
<td align = "right"><asp:LinkButton ID="lnkbFirst" runat="server" Text = "First" ForeColor = "#0099FF" CommandName = "First" OnCommand = "ChangePage" ></asp:LinkButton></td>
<td></td>
<td align = "left"><asp:LinkButton ID="lnkbPrevious" runat="server" Text = "Prevoius" CommandName = "Previous" OnCommand="ChangePage" ForeColor = "#0099FF"></asp:LinkButton></td>
<td align = "right"></td>
<td></td>
<td align = "right"><asp:LinkButton ID="lnkbNext" runat="server" Text = "Next" CommandName = "Next" OnCommand="ChangePage" ForeColor = "#0099FF"></asp:LinkButton></td>
<td></td>
<td align = "left"><asp:LinkButton ID="lnkbLast" runat="server" Text = "Last" ForeColor = "#0099FF" OnCommand="ChangePage" CommandName = "Last"></asp:LinkButton></td>
</tr>
</table>


step3://----------- in the code behind file (c#)---------
//declare this in globaly
Connection connectionDb = new Connection(); // generate an object of class, connection
DataSet dsRetrieve = new DataSet();
DataSet dsPaging = new DataSet();
int currentPageNumber;
private const int pageSize =5;

// set the paging button enable and disable
public void ApplayPaging()
{
// to matain the current page number between the post back
currentPageNumber = (int)ViewState["currentPageNumber"];
int totalPages;
int maxRows = 0;
dsRetrieve = connectionDb.Retrieve(); // retrieve the records
maxRows = dsRetrieve.Tables[0].Rows.Count;
if (maxRows <= pageSize)
{
lnkbPrevious.Enabled = false;
lnkbFirst.Enabled = false;
lnkbNext.Enabled = false;
lnkbLast.Enabled = false;
}
totalPages = CalculateTotalPages(maxRows); // to find the total pages in the grid view
if (currentPageNumber == 1)
{
lnkbPrevious.Enabled = false;
lnkbFirst.Enabled = false;
if (totalPages > 1)
{
lnkbNext.Enabled = true;
lnkbLast.Enabled = true;
}
else
{
lnkbNext.Enabled = false;
lnkbLast.Enabled = false;
}
}
else
{
lnkbPrevious.Enabled = true;
lnkbFirst.Enabled = true;
if (currentPageNumber >= totalPages)
{
lnkbNext.Enabled = false;
lnkbLast.Enabled = false;
}
else
{
lnkbNext.Enabled = true;
lnkbLast.Enabled = true;
}
}
}

// to calculate the number of pages in a grid view
public int CalculateTotalPages(int maxRows)
{
float maxRecords = (float)maxRows;
float Pages = maxRecords / pageSize;
int totalPage = (int)Pages;
float point = Pages - (int)Pages;
if (point < 1 && point > 0)
{
totalPage = totalPage + 1;
}
return totalPage;
}

// call this function using link button onCommand event..
// navigation button in paging
protected void ChangePage(object sender, CommandEventArgs e)
{
currentPageNumber = (int)ViewState["currentPageNumber"];
int totalPages;
int maxRows = 0;
dsRetrieve = connectionDb.Retrieve(); // retrieve the records

// to find the total number of records in the table
maxRows = dsRetrieve.Tables[0].Rows.Count; //

// find the total pages in the grid view
totalPages = CalculateTotalPages(maxRows);
switch (e.CommandName)
{
case "Previous":
currentPageNumber = currentPageNumber - 1;
break;
case "Next":
currentPageNumber = currentPageNumber + 1;
break;
case "First":
currentPageNumber = 1;
break;
case "Last":
currentPageNumber = totalPages;
break;
}
ViewState["currentPageNumber"] = currentPageNumber;
fillGrid(); // bind data values to the grid view
}

private void fillgrid()
{
ApplayPaging();
currentPageNumber = (int)ViewState["currentPageNumber"];
dsPaging = connectionDb.Paging(pageSize, currentPageNumber); // call the paging function
// bind the dataset to the gridview
grdManupulate.DataSource = dsPaging;
grdManupulate.DataBind();
}

//page load
protected void Page_Load(object sender, EventArgs e)
{ // execute for the first page load
if (!IsPostBack)
{
currentPageNumber = 1;
ViewState["currentPageNumber"] = currentPageNumber;
Fillgrid(); // bind data values to the grid view
}
}

Comments or Responses

Posted by: Nithadeepak on: 4/26/2011 Level:Bronze | Status: [Member] | Points: 10
Hi Biju,
Place your codes inside code tags. It will look good.

Thanks,
Nitha

Login to post response