2011-12-26 183 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); 
}