将 GridView 与许多记录绑定 [英] Bind GridView with many records

查看:14
本文介绍了将 GridView 与许多记录绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想要延迟加载或虚拟化堆栈的功能wpf 中的面板.

这就是我绑定到 GridView 的方式 -

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

推荐答案

这里是,根据你的变化,我会怎么做:

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

现在在您的页面中;

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

这篇关于将 GridView 与许多记录绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆