handsomeboy
handsomeboy

Reputation: 35

How to display a huge amount of data in a grid view without crashing the page?

i am using linq for my connection to the database and this is my first time to display a large amount of data from the database so i don't how to handle it..can you give me a tip? thanks!

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

        rebind();
    }

    private void rebind()
    {
        using ( var db = new linqDataContext())
        {
            GridView1.DataSource = db.Orders.Select(p => new { p.OrderID, p.CustomerID, p.ShipName, p.ShipCity }).ToList();
            GridView1.DataBind();
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        rebind();
    }
}

Upvotes: 0

Views: 3325

Answers (4)

Anil Mathew
Anil Mathew

Reputation: 2696

For paging through large amount of data, you need to make use of custom paging.

Custom paging ensures that only the precise set of records needed for a particular page of data is retrieved from the database at a time.

The following link explains in detail how to perform custom paging using the ObjectDataSource control: http://msdn.microsoft.com/en-us/library/bb445504.aspx

Upvotes: 2

Joseph Victor Zammit
Joseph Victor Zammit

Reputation: 15320

Paging. Look at two amazing JQuery based tables (that handle a whole lot of other stuff, for you, like ordering and filtering):

Upvotes: 1

TheDPQ
TheDPQ

Reputation: 486

Have you thought about paging? You should be able to bind the LINQ results to the Gridview datasource. I believe all you need to do is set the pagesize on the gridview and it will work.

I've done it before but i actually keep track of the page (and pagesize) myself in code behind and had LINQ something like gv.DataSource = (From s In Results Select s).Skip(pageSize * (currPage-1)).Take(pageSize)

Upvotes: 2

Bernard
Bernard

Reputation: 7961

Tip: Don't display a lot of data at once in your web application. Use a paging control to retrieve and display only a few rows from the database at a time.

Upvotes: 5

Related Questions