2011-12-26 204 views
0

我将GridView的源设置为存储过程的结果,该存储过程基本上是可数据的。ASPxGridView数据缓存

ASPxGridView1.DataSource = dt1; 
ASPxGridView1.DataBind(); 

这发生在点击按钮上。第一步可以,但是当我尝试排序,过滤或转到下一页的结果时,gridview是空的,我必须单击按钮(显然调用DataBind)才能看到结果。

所以,我的问题是,如何以某种方式缓存来自存储过程的数据表,所以我不需要绑定任何排序或页面变化的数据。

谢谢。

+0

您是否检查过MSDN? [与数据源控件缓存](http://msdn.microsoft.com/en-us/library/ms227994.aspx) – sq33G 2011-12-26 12:25:05

回答

4

一般来说,当你更新在运行时ASPxGridViewWebChartControl数据,这些信息不会自动缓存。

因此,您必须使用事件处理程序向服务器发送每个请求时将其提供给控件。

要提高性能,可以将数据保存到Session/Cache 变量中。

仅供参考检查以下的DevExpress KB关于你的问题,KB:
rebinding gridview at each postback
Bind a grid to a DataTable via code.
Why might paging (sorting, grouping, filtering) not work in the ASPxGridView?

//缓存数据表中的会话,以避免多个数据库打在每个回发

DataTable GetTable() { 
      //You can store a DataTable in the session state 
      DataTable table = Session["Table"] as DataTable; 
      if (table == null) { 
       table = new DataTable(); 
       table.Columns.Add("id", typeof(int)); 
       table.Columns.Add("data", typeof(String)); 
       for (int n = 0; n < 100; n++) { 
        table.Rows.Add(n, "row" + n.ToString()); 
       } 
       Session["Table"] = table; 
      } 

      //Otherwise you have to create a DataTable instance on every request: 
      //DataTable table = new DataTable(); 
      //table.Columns.Add("id", typeof(int)); 
      //table.Columns.Add("data", typeof(String)); 
      //for(int n = 0; n < 100; n++) { 
      // table.Rows.Add(n, "row" + n.ToString()); 
      //} 

      return table; 
     } 

引用链接到改善的DevExpress GridView的控制性能:
Default grid data binding behavior is unworkable for large data sets
ASPxGridView.DataSourceForceStandardPaging Property
ASPxGridView - How to implement caching using the SqlCacheDependency or SqlDependency classes
Best Solution for ASPxGridView with Custom DataTable objects
Data caching on the client side
How To Cache Rows In DevExpress ASP.NET GridView
Performance issue in GridView when using paging & master detail view
Speed up your page loads with a lighter ViewState
Data Loading times and custom record loading

+0

美丽的解决方案。非常感谢你! – 2012-01-09 09:44:01

+1

如果我的Datatable包含超过5.000行,该怎么办?在会议上存储它是否是一个很好的实践? – 2012-03-30 13:02:39

0

使用的ViewState

步骤1:

GridView1.DataSource = ds; 
ViewState["itemsetPending"] = ds; 
GridView1.DataBind(); 

步骤2:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    DataSet m_DataTable = (DataSet)ViewState["itemsetPending"]; 

    if (m_DataTable != null) 
    { 
     DataView m_dataview = new DataView(m_DataTable.Tables[0]); 

     if (Convert.ToInt32(ViewState["m_sort"]) == 0) 
     { 
      m_dataview.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(SortDirection.Descending); 
      ViewState["m_sort"] = 1; 
     } 
     else 
     { 
      m_dataview.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); 
      ViewState["m_sort"] = 0; 
     } 

     GridView1.DataSource = m_dataview; 
     GridView1.DataBind(); 
    } 
} 

这里我米从存储过程中的数据填充到数据集和分配成ViewState.Just试试吧。

private string ConvertSortDirectionToSql(SortDirection sortDirection) 
{ 
    string newSortDirection = String.Empty; 

    switch (sortDirection) 
    { 
     case SortDirection.Ascending: 
      newSortDirection = "ASC"; 
      break; 

     case SortDirection.Descending: 
      newSortDirection = "DESC"; 
      break; 
    } 

    return newSortDirection; 
} 
1

您可以使用AfterPerformCallback

protected void gridSchedule_AfterPerformCallback(object sender, 
     DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e) 
{ 
    LoadGrid(gridSchedule); 
}