简体   繁体   中英

Slow population of data using GridView in c# asp.net

I am working in a web application in asp.net using c# and I want to display a large amount of rows from 10.000 - 1.000.000. The data bind fast, as I checked in step-by-step in debug mode, but when the page tries to display the GridView it takes forever. Any suggestions?

The problem is when clients (browsers) try do display (render) a large amount of GUI. Asking the client to render 10,000 - 1,000,000 rows can definitly take way too much time.

One solution to your problem is using paging using the AllowPaging with PageSize properties. This will limit the number of rows displayed in your GridView to what you set in the PageSize property.

HTML :

<asp:GridView   ID="gv" 
                runat="server" 
                OnPageIndexChanging="gv_PageIndexChanging"
                OnPageIndexChanged="gv_PageIndexChanged"
                AllowPaging="true"
                PageSize="50">
        <Columns>
                ....

Code behind :

protected void gv_PageIndexChanged(object sender, EventArgs e)
{
    // bind your data
    gv.DataBind();
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    if (e.NewPageIndex == -1)
        gv.PageIndex = Int32.MaxValue;
    else
        gv.PageIndex = e.NewPageIndex;
}

Recommendation : Using the above solution should solve the UI problem, but the huge amount data will still be fetched . I suggest paging both in the SQL level and in the UI level .

You should design a query for this problem, for example you want to display 10 records from 10,00000 record on each page in your gridview paging.

Pass three parameters ('@startRowIndex', '@startRowIndex', '@maximumRows') from your gridview in below select statement and use ROW_NUMBER() method (SQL Server 2005 above supports it) to get sequentially-increasing row number.

SELECT ...
FROM
(SELECT ... 
     ROW_NUMBER() OVER(ORDER BY ColumnName) as RowNum
FROM Employees e
) as DerivedTableName
WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1

Link 1: http://www.4guysfromrolla.com/articles/031506-1.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM