How to do custom paging in Gridview using “LinqDataSource” control ? Part #6


Hi

Gridview support default paging feature. But if we have to fetch huge no of data and we are using default paging feature, then it will fetch all data on every page click event, so it will decrease the performance of database and sometime,it will give connection failure errors.

To avoid the database server overload, we have to create custom paging which will fetch  only require no of data at one time.

There are  two method to do this task

  1. Using store procedure
  2. Using LINQ

If we are working on asp.net 3.5 then we can create Custom paging with a few line of code.

This is the few steps to do this task

Step 1: Take a gridview and Linqdatasource control in aspx page

Step2: Configure the Gridview control like this

<asp:GridView ID=”Gridview1″ runat=”server” DataSourceID=”LinqDataSource1″

AllowPaging=”true” PageSize=”<%# PAGE_SIZE %>”

AutoGenerateColumns=”false” Width=”276px”>

<Columns>

<asp:BoundField DataField=”Id” HeaderText=”Id”        InsertVisible=”False”

ReadOnly=”True” SortExpression=”Id” />

<asp:BoundField DataField=”EmpName” HeaderText=”EmpName”

SortExpression=”EmpName” />

<asp:BoundField DataField=”EmpSal” HeaderText=”EmpSal”

SortExpression=”EmpSal” />

</Columns>

</asp:GridView

Step3: Configure the LinqDataSource Control like this

<asp:LinqDataSource ID=”LinqDataSource1″ runat=”server”

ContextTypeName=”Namespace.MyDataContext”  AutoPage=”false”

onselecting=”LinqDataSource1_Selecting”>

</asp:LinqDataSource>

Step 4: Create a DataClassesDataContext using  “LINQ to SQL Class” and fill the desire table in that class. If you don’t know then please do Like this post.

Step 5: Write the code in code behind file like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

public const int PAGE_SIZE = 10;

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
// LINQ query

DataClassesDataContext dc = new DataClassesDataContext();
var query = from m in dc.tblEmps
select m;

// Set the total count
// so GridView knows how many pages to create
e.Arguments.TotalRowCount = query.Count();

// Get only the rows we need for the page requested
query = query.Skip(Gridview1.PageIndex * PAGE_SIZE).Take(PAGE_SIZE);

e.Result = query;
}

}

Then we will get the output like this

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.