user5201074
user5201074

Reputation:

linq to sql gridview paging server side

I have table having large number of records which is bound to a Gridview. Currently it binds all the data at page load. I'm using following LINQ to SQL:

gvView.datasource = from c in _db.tblCategories
                        where c.bIsDeleted == false
                        select c;
gvView.databind();

I want to convert this with paging so that it doesn't show all the data at once.

Upvotes: 0

Views: 1698

Answers (2)

Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

You need to use skip and take linq methods. Please follow this link for example http://msdn.microsoft.com/en-us/library/bb386988.aspx

Here is a sample, but go through the link for better understanding

var custQuery2 =
    (from cust in db.Customers
    orderby cust.ContactName
    select cust)
    .Skip((currentpagenumber-1)*pagesize).Take(pagesize);

To use above code you need to change your approach. You need to to determine current page number and size based on user's selection, execute this code and rebind it to gridview.

Upvotes: 2

A_Sk
A_Sk

Reputation: 4630

.CS Code

gvView.datasource = (from c in _db.tblCategories
                        where c.bIsDeleted == false
                        select c).ToList();
gvView.databind();

.ASPX Code for GridView:

<asp:GridView ID="gvView" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    OnPageIndexChanging="OnPageIndexChanging" PageSize="10">
    <Columns>
        <asp:BoundField ItemStyle-Width="150px" DataField="myField1" HeaderText="myField1" />
    </Columns>
</asp:GridView>

See This for GridView Pagination.

Upvotes: 0

Related Questions