Rohit Vats
Rohit Vats

Reputation: 81243

Bind GridView with many records

I have a GridView which shows only 50 records at a time through paging. If i bind that to a source say containing 150 records, it works like charm but when the record size increases drastically to 10,00000, it took ages to load data.. So, i want to know is there any way of just loading 50 records at a time which are visible??

I want something like Lazy Loading or the feature of Virtualizing Stack Panel in wpf.

This is how i am binding to the 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();  
        }

And here's the function which fetch data from 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()
                };
        }

Upvotes: 1

Views: 1411

Answers (1)

Simon Dugré
Simon Dugré

Reputation: 18916

Here is, accoring to your change, how I would do that :

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();
}

And now in your page ;

<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>

Upvotes: 1

Related Questions