2011-04-08 36 views
1

我有一個GridView,一次只顯示50條記錄,通過分頁。如果我將它綁定到包含150條記錄的源文件,它就像魅力一樣工作,但是當記錄大小急劇增加到10,00000時,它花費了很長時間才能加載數據。所以,我想知道是否有任何方法只是加載50記錄在一個可見的時間?綁定GridView有很多記錄

我想是懶加載或WPF中的虛擬化堆棧 面板的功能。

我這是怎麼結合到GridView -

private void LoadCustomers() 
     {    
      if (Cache["CustomersData"] == null) 
      { 
       Business.Customers customers = Business.Customers.GetAllCustomer(); 
       Cache["CustomersData"] = customers; 
      } 
      this.customerGridView.DataSource = Cache["CustomersData"]; 
      this.customerGridView.DataBind(); 
     } 

下面是從DB-

獲取數據
public static Customers GetAllCustomer(int index) 
     { 
      Customers customers = new Customers(); 

     NShop_SmallEntities data = new NShop_SmallEntities(); 
     var dbCustomers = (from c in data.Customers 
          select c).OrderBy(c=> c.CustomerId).Skip(index * 50).Take(50); 

     foreach (var dbCustomer in dbCustomers) 
     { 
      customers.Add(Customer.GetCustomer(dbCustomer)); 
     } 

     return customers; 
     } 

public static Customer GetCustomer(DAL.Customer dbCustomer) 
     { 
      return new Customer() 
       { 
        CustomerId = dbCustomer.CustomerId, 
        FirstName = dbCustomer.FirstName, 
        LastName = dbCustomer.LastName, 
        Email = dbCustomer.Email, 
        DOB = dbCustomer.DOB, 
        City = dbCustomer.City, 
        State = dbCustomer.State, 
        PostalCode = dbCustomer.PostalCode, 
        Country = dbCustomer.Country, 
        OrderCount = dbCustomer.Orders.Count() 
       }; 
     } 
+0

什麼樣的DB的:SQL Server中,AS400 ...... – IrishChieftain 2011-04-08 19:40:53

+0

數據庫是SQL服務器... – 2011-04-08 19:49:08

回答

1

這裏,accoring對你的變化,功能如何我會這樣做:

public static Customers GetAllCustomer(int startIndex, int nbrOfResults, out total) { 
    Customers customers = new Customers(); 

    NShop_SmallEntities data = new NShop_SmallEntities(); 
    var dbCustomers = from c in data.Customers 
        select c; 

    // Retreiving total number of customers. NEEDED to get 
    // ObjectDataSource working. 
    total = dbCustomers.Count(); 

    foreach (var dbCustomer in dbCustomers 
         .Skip((startIndex * nbrOfResults) + 1) 
         .Take(NumberOfResults).ToList() { 
     customers.Add(Customer.GetCustomer(dbCustomer)); 
    } 
    return customers; 
} 

/// <summary> 
/// This methods must have the same signature than the "real" one... exept the name :oP 
/// </summary> 
public static int GetAllCustomerCount(int startIndex, int nbrOfResults, out total) { 
     return (from c in data.Customers select c).Count(); 
} 

而且現在在你的頁面中;

<asp:GridView ID="gvBulletins" runat="server" AllowPaging="True" 
    ObjectDataSourceID="objCustomersDS"> 
</asp:GridView> 

<asp:ObjectDataSource ID="objCustomersDS" runat="server" 
    SelectMethod="GetAllCustomer" SelectCountMethod="GetAllCustomerCount" 
    TypeName="AssemblyName" EnablePaging="True" MaximumRowsParameterName="nbrOfResults" 
    StartRowIndexParameterName="startIndex"> 
    <SelectParameters> 
     <asp:Parameter Name="startIndex" Type="Int32" /> 
     <asp:Parameter Name="nbrOfResults" Type="Int32" /> 
     <asp:Parameter Direction="Output" Name="total" Type="Int32" /> 
    </SelectParameters> 
</asp:ObjectDataSource> 
+0

謝謝,但在這不是我期待的。我已經更新了我的問題,以便更加準確。 – 2011-04-08 19:34:16

+0

但您如何綁定您的源代碼?它是實體,SQL等?如果它是一個對象數據源,那可能是因爲你返回了所有的數據,並且在你把它收回之後。 – 2011-04-08 19:37:22

+0

用一些代碼更新了我的問題... – 2011-04-08 19:49:39