在gridview中加载数据很慢

时间:2012-09-14 09:17:42

标签: asp.net linq

获得如下结果并显示第2列(用户控制)&第3列(用户控件)按列1分组。我已经制作了如下代码,但它很慢,实际上当我只使用第1列时,加载很快(5秒),当我使用第1列和第1列时column2(用户控件“文档列表”)加载是10秒,当我使用第1列和第1列时第2列(文档列表“)& column3(”DocTransList“)加载是45秒。我认为LINQ中的代码效率不高,但我不知道如何解决它。

TRANSMITTAL No        Documents           REV
TT-001                DOC-001              01
                      DOC-002              01
public partial class Transmittals : System.Web.UI.Page
{

  class TransmittalPresentationModel
  {
    public int TransID { get; set; }
    public String TransmittalNo { get; set; }
    public IEnumerable<tblDocument> Documents { get; set; }
    public IEnumerable<tblTransmittall> DocTrans { get; set; }
  }

  private EDMSDataContext _DataContext;

  protected void Page_Load(object sender, EventArgs e)
  {
    _DataContext = new EDMSDataContext();
    if (!IsPostBack)
    {
      DisplayAuthors();
    }
  }

  private void DisplayAuthors()
  {
    var query =
      from transmittal in _DataContext.tbltransmittalNos
      orderby transmittal.TRANSMITTAL
      select new TransmittalPresentationModel
      {
        TransID = transmittal.TransID,
        TransmittalNo = transmittal.TRANSMITTAL,
        Documents = transmittal.tblTransmittalls.Select(Transmittals => Transmittals.tblDocument),
        DocTrans = transmittal.tblTransmittalls

      };
    GridViewTransmittals.DataSource = query.ToList();
    GridViewTransmittals.DataBind();
  }

  protected void GridViewTransmittals_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.DataItem == null) return;

    TransmittalPresentationModel transmittal = (TransmittalPresentationModel)e.Row.DataItem;

    DocumentList documentList = (DocumentList)e.Row.FindControl("DocumentList1");
    DocTransList doctranslist = (DocTransList)e.Row.FindControl("DocTransList2");

    documentList.Documents = transmittal.Documents;
    documentList.DataBind();

    doctranslist.DocTrans = transmittal.DocTrans;
    doctranslist.DataBind();
  }
}

1 个答案:

答案 0 :(得分:0)

您遇到了问题,因为您的记录都需要在每页加载/刷新时都加载到GridView中。这会从数据库中提取数据,并且每次都绘制每一行,这非常耗时。

更好的选择是使用Ajax加载GridView。

或在服务器端使用分页,仅将数据提供给所需的页面。