C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Paging in asp.net - part II (Custom Paging in datagrid).


Posted Date: 10 Nov 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Atal Bihari UpadhyayMember Level: Gold    
Rating: 1 out of 5Points: 10



Problem with automatic Paging:
When data is displayed in the DataGrid control, datasource is loaded
every time the DataGrid control moves to a different page. This can consume a lot of resources when the data source is very large resulting in degraded performance.

Custom paging allows to load just the segment of data needed to display a single page. By using custom paging, data is retrieved in page-size "chunks" i.e.
retrieve just a page data which needs to be displayed.
To do that, you turn off the automatic paging feature of the grid so that it doesn't assume that it is working with the entire dataset. We then get the correct number of rows to fill the grid.

AllowCustomPaging property of datagrid is used for enabling custom paging in a datagrid.

What is VirtualItemCount property:

This property of datagrid is used to specify the virtual number of items in the DataGrid control when custom paging is used.

How to retrieve only requird records:

Suppose you have specified pagesize as n.
Records for 1st page: 1 to x
Records for 2nd page: x+1 to xn
Records for 3rd page: 2x+1 to xn


Records for x page: (n-1 )*x+1 to x*n

Sql for retrieving nth page records:

If we are able to arrange records by id in sequence no. (1,2,3,4,5,….), the sql for retrieving nth page should look like:

Select field1, field2 … from table where id> x*n and
id <= (n+1)* x
Where n=pagesize
X=page no.

How to create unique id for records:

For creating unique id for records, we can create a temporary table as below:

CREATE TABLE #Temp
(
RecID int IDENTITY(1,1),
FieldId int
)

We need to populate the temporary from main table as:

INSERT INTO #Temp (FieldId)
SELECT FieldId
FROM Table
ORDER BY Field

Once temporary table is populated, we can write the select query to retrieve records for nth page as below:

SELECT table.*
FROM #Temp a, table b
Where
a.FieldId = b.fieldId
WHERE RecId > @PageSize * @CurrentPage
AND RecId <= @PageSize * (@CurrentPage+1)

We can also use the query like:

SELECT top @pagesize table.*
FROM #Temp a, table b
Where
a.FieldId = b.fieldId
WHERE RecId > @PageSize * @CurrentPage

Example:


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>

<script runat="server">

int startIndex = 0;

ICollection CreateDataSource()
{

// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;

// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(string)));
dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));

// Populate the table with sample values. When using custom paging,
// a query should only return enough data to fill a single page,
// beginning at the start index.
for (int i = startIndex; i < (startIndex + MyDataGrid.PageSize); i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = DateTime.Now.AddDays(i).ToShortDateString();
dr[3] = (i % 2 != 0) ? true : false;

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

}

void Page_Load(Object sender, EventArgs e)
{

// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{

// Set the virtual item count, which specifies the total number
// items displayed in the DataGrid control when custom paging
// is used.
MyDataGrid.VirtualItemCount = 200;

// Retrieve the segment of data to display on the page from the
// data source and bind it to the DataGrid control.
BindGrid();

}

}

void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{


MyDataGrid.CurrentPageIndex = e.NewPageIndex;

startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;

BindGrid();

}

void BindGrid()
{

MyDataGrid.DataSource = CreateDataSource();
MyDataGrid.DataBind();

}

</script>

<body>

<form runat="server" ID="Form1">

<h3> DataGrid Custom Paging Example </h3>

<asp:DataGrid id="MyDataGrid"
AllowCustomPaging="True"
AllowPaging="True"
PageSize="10"
OnPageIndexChanged="MyDataGrid_Page"
runat="server">

<HeaderStyle BackColor="Navy"
ForeColor="White"
Font-Bold="True" />

<PagerStyle Mode="NumericPages"
HorizontalAlign="Right" />

</asp:DataGrid>

</form>

</body>
</html>


Hope it helps in improving the performance of the pages where large data is displayed using datagrid.

Your comments are welcome.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to capture the process time of an aspx page
Previous Resource: Document Management System using ASP.Net & Sql Server
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use