将GridView与许多记录绑定

时间:2011-04-08 19:07:40

标签: asp.net gridview

我有一个GridView,它通过分页一次只显示50条记录。如果我把它绑定到包含150条记录的源,它就像魅力一样,但当记录大小急剧增加到10,00000时,加载数据需要很长时间..所以,我想知道是否有任何方法只需加载50一次可见的记录??

  

我想要一些像Lazy Loading或Virtualizing Stack的功能   小组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()
                };
        }

1 个答案:

答案 0 :(得分:1)

根据你的改变,我会怎么做:

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>