0

我正在使用 linq 连接到数据库,这是我第一次显示来自数据库的大量数据,所以我不知道如何处理它..你能给我提示吗?谢谢!

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();
    }
}
4

4 回答 4

5

提示:不要在您的 Web 应用程序中一次显示大量数据。使用分页控件一次只检索和显示数据库中的几行。

于 2012-04-14T00:40:09.563 回答
2

你有没有考虑分页?您应该能够将 LINQ 结果绑定到 Gridview 数据源。我相信您需要做的就是在 gridview 上设置页面大小,它就会起作用。

我以前做过,但实际上我自己在后面的代码中跟踪页面(和页面大小)并且有类似 LINQ 的东西 gv.DataSource = (From s In Results Select s).Skip(pageSize * (currPage-1)).Take(pageSize)

于 2012-04-14T00:45:09.680 回答
2

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

于 2012-04-16T11:27:38.200 回答
1

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

于 2012-04-15T16:25:51.353 回答