Asp.net 用多条记录绑定GridView
我有一个GridView,它通过分页一次只显示50条记录。如果我将其绑定到一个包含150条记录的源上,它的工作方式就像charm一样,但是当记录大小急剧增加到1000000条时,加载数据需要花费很长时间。。所以,我想知道有没有办法一次只加载50条可见的记录 我想要延迟加载或虚拟化堆栈的功能 wpf中的面板。 这就是我绑定到GridView的方式-Asp.net 用多条记录绑定GridView,asp.net,gridview,Asp.net,Gridview,我有一个GridView,它通过分页一次只显示50条记录。如果我将其绑定到一个包含150条记录的源上,它的工作方式就像charm一样,但是当记录大小急剧增加到1000000条时,加载数据需要花费很长时间。。所以,我想知道有没有办法一次只加载50条可见的记录 我想要延迟加载或虚拟化堆栈的功能 wpf中的面板。 这就是我绑定到GridView的方式- private void LoadCustomers() { if (Cache["C
private void LoadCustomers()
{
if (Cache["CustomersData"] == null)
{
Business.Customers customers = Business.Customers.GetAllCustomer();
Cache["CustomersData"] = customers;
}
this.customerGridView.DataSource = Cache["CustomersData"];
this.customerGridView.DataBind();
}
这是从数据库中获取数据的函数-
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()
};
}
以下是根据您的变化,我将如何做到这一点:
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();
}
publicstaticcustomers GetAllCustomer(int-startIndex,int-nbroresults,out-total){
客户=新客户();
NShop_SmallEntities data=新的NShop_SmallEntities();
var dbCustomers=data.Customers中的c
选择c;
//正在检索客户总数。需要获取
//ObjectDataSource正在工作。
total=dbCustomers.Count();
foreach(dbCustomers中的var dbCustomer
.Skip((startIndex*nbroResults)+1)
.Take(NumberOfResults).ToList(){
customers.Add(Customer.GetCustomer(dbCustomer));
}
返回客户;
}
///
///此方法必须具有与“真实”方法相同的签名…除了名称:oP
///
公共静态int GetAllCustomerCount(int startIndex、int NBROFRESSULT、out总计){
返回(从数据中的c.Customers选择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>
根据您的变化,我将如何做到这一点:
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();
}
publicstaticcustomers GetAllCustomer(int-startIndex,int-nbroresults,out-total){
客户=新客户();
NShop_SmallEntities data=新的NShop_SmallEntities();
var dbCustomers=data.Customers中的c
选择c;
//正在检索客户总数。需要获取
//ObjectDataSource正在工作。
total=dbCustomers.Count();
foreach(dbCustomers中的var dbCustomer
.Skip((startIndex*nbroResults)+1)
.Take(NumberOfResults).ToList(){
customers.Add(Customer.GetCustomer(dbCustomer));
}
返回客户;
}
///
///此方法必须具有与“真实”方法相同的签名…除了名称:oP
///
公共静态int GetAllCustomerCount(int startIndex、int NBROFRESSULT、out总计){
返回(从数据中的c.Customers选择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>
谢谢,但这不是我想要的。我已经更新了我的问题,使之更精确。但是您如何绑定到源?是实体、SQL等吗?如果它是对象数据源,可能是因为您返回了所有数据,然后将其舀入其中。然后在GetAllCustomer函数中,为fe添加skip和take参数tch.Otherwize,如果您有1000000条记录,它将返回10000000条记录。Take和skip将精确地定义这些记录,并仅从DB获取这些记录…feltching 50-100-150条记录和1000000条记录之间存在差异!!我已更新了GetAllCustomer,使其一次仅获取50条记录,但我现在面临的问题是,分页是gone用于网格视图,因为现在网格视图的数据源仅包含50条记录,因此没有分页…:((是的……这就是为什么在使用对象类型源时,您需要使用objectdatasource,而不是将其直接绑定到coutrol的数据源中。您是否可以使用objectdatasource?因为在使用此方法时,您将通过添加返回整数(而不是对象)的“CountMethod”手动完成所有分页操作)在您的例子中,“(从数据中的c.Customers选择c.Count();”谢谢,但这并不是我想要的。我已经更新了我的问题,使之更精确。但是您如何绑定到源?是实体、SQL等吗?如果它是对象数据源,可能是因为您返回了所有数据,然后将其舀入其中。然后在GetAllCustomer函数中,为fetch.Ot添加skip和take参数herwize,如果您有1000000条记录,它会返回10000000条记录。Take and skip将精确地定义这些记录,并仅从DB中获取这些记录…feltching 50-100-150条记录与1000000条记录之间存在差异!!我已更新了GetAllCustomer,使其一次仅获取50条记录,但我现在面临的问题是,t的分页已消失之所以选择网格视图,是因为现在网格视图的数据源只包含50条记录,因此没有分页…:((是的……这就是为什么在使用对象类型源时,您需要使用objectdatasource,而不是将其直接绑定到coutrol的数据源中。您是否可以使用objectdatasource?因为在使用此方法时,您将通过添加返回整数(而不是对象)的“CountMethod”手动完成所有分页操作)在您的例子中,“(从数据中的c.Customers选择c.Count();”什么类型的数据库:SQL Server,AS400…什么类型的数据库:SQL Server,AS400。。。