绑定的GridView有许多记录 [英] Bind GridView with many records
问题描述
我有一个GridView,显示只有50条记录在通过分页的时候。如果我将绑定到一个源说包含150的记录,它的工作原理般的魅力,但是当记录尺寸急剧增加至10,00000,它经历了千百年来加载数据。所以,我想知道的是有刚装50的任何方式在一个时间该可见??记录
我想是懒加载或虚拟堆栈的功能
面板中的WPF。
块引用>这就是我如何绑定到GridView -
私人无效LoadCustomers()
{
如果(高速缓存[CustomersData] == NULL)
{
Business.Customers客户= Business.Customers.GetAllCustomer();
缓存[CustomersData] =客户;
}
this.customerGridView.DataSource =缓存[CustomersData];
this.customerGridView.DataBind();
}下面是它获取来自DB-数据的功能。
公共静态客户GetAllCustomer(INT指数)
{
客户的客户=新客户(); NShop_SmallEntities数据=新NShop_SmallEntities();
VAR DBCUSTOMERS =(从C在data.Customers
选择C).OrderBy(C => c.CustomerId).Skip(指数* 50)。取(50); 的foreach(在DBCUSTOMERS VAR dbCustomer)
{
customers.Add(Customer.GetCustomer(dbCustomer));
} 回报客户;
}公共静态客户GetCustomer(DAL.Customer dbCustomer)
{
返回新客户()
{
客户ID = dbCustomer.CustomerId,
名字= dbCustomer.FirstName,
姓氏= dbCustomer.LastName,
电子邮件= dbCustomer.Email,
DOB = dbCustomer.DOB,
市= dbCustomer.City,
状态= dbCustomer.State,
邮政code = dbCustomer.Postal code,
国家= dbCustomer.Country,
定单计数= dbCustomer.Orders.Count()
};
}
解决方案下面是,accoring对你的变化,我会怎么做:
公共静态客户GetAllCustomer(INT的startIndex,诠释nbrOfResults,出总){
客户的客户=新客户(); NShop_SmallEntities数据=新NShop_SmallEntities();
从data.CustomersÇVAR DBCUSTOMERS =
选择C; // Retreiving客户总数。需要得到
// ObjectDataSource的工作。
总= dbCustomers.Count(); 在DBCUSTOMERS的foreach(VAR dbCustomer
.Skip((的startIndex * nbrOfResults)+1)
。取(NumberOfResults).ToList(){
customers.Add(Customer.GetCustomer(dbCustomer));
}
回报客户;
}///<总结>
///此方法必须具有比真正的一相同的签名...... exept名称:oP处
///< /总结>
公共静态INT GetAllCustomerCount(INT的startIndex,诠释nbrOfResults,出总){
返回(从C中选择data.Customers C).Count之间的();
}现在在你的页面;
< ASP:GridView控件ID =gvBulletins=服务器AllowPaging =真
ObjectDataSourceID =objCustomersDS>
< / ASP:GridView的>< ASP:ObjectDataSource控件ID =objCustomersDS=服务器
SelectMethod =GetAllCustomerSelectCountMethod =GetAllCustomerCount
类型名=的AssemblyNameEnablePaging =真MaximumRowsParameterName =nbrOfResults
StartRowIndexParameterName =的startIndex>
< SelectParameters>
< ASP:参数名称=的startIndexTYPE =的Int32/>
< ASP:参数名称=nbrOfResultsTYPE =的Int32/>
< ASP:参数方向=输出NAME =总TYPE =的Int32/>
< / SelectParameters>
< / ASP:ObjectDataSource控件>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() }; }
解决方案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>
这篇关于绑定的GridView有许多记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!