2011-04-08 51 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