[英]Bind a <List> to an ObjectDataSource (c# and Entity Framework)
I have a GridView that I'm binding from my database table "Publishers". 我有一个GridView,我从我的数据库表“Publishers”绑定。 The "Publishers" table has 22,000+ rows inside it and every bind event is taking a LONG time.
“Publishers”表中有22,000多行,每个绑定事件都需要很长时间 。
I have implemented custom paging by using the following technique: 我使用以下技术实现了自定义分页:
public class PublisherDataSource
{
public List<Publisher> GetPublishers(int startIndex, int maxRows)
{
using (CA_Entities db = new CA_Entities ())
{
return (from publisher in db.Publishers
select publisher)
.Where(p => p.isActive == true)
.OrderByDescending(p => p.CreatedDT)
.ThenBy(p => p.SiteURL)
.Skip(startIndex)
.Take(maxRows).ToList();
}
}
public int GetPublishersCount()
{
using (CA_Entities db = new CA_Entities ())
{
return db.Publishers.Count();
}
}
}
And in my code behind of the grid control: 在我的代码后面的网格控件:
CA_Entities db = new CA_Entities ();
// Custom paging, to make DB access & page response quicker
PublisherDataSource pds = new PublisherDataSource();
ObjectDataSource ods = new ObjectDataSource();
ods.EnablePaging = true;
ods.SelectMethod = "GetPublishers";
ods.SelectCountMethod = "GetPublishersCount";
ods.TypeName = pds.ToString();
ods.MaximumRowsParameterName = "maxRows";
ods.StartRowIndexParameterName = "startIndex";
gridPublishers.DataSource = ods;
gridPublishers.DataBind();
This code works brilliantly, but before I finally bind the grid, I want to manipulate the results from the db 这段代码非常出色,但在我最终绑定网格之前,我想操纵db的结果
eg. 例如。 Removing certain records, further sorting & filtering of records.
删除某些记录,进一步排序和过滤记录。
So, Ideally, I'd like to say something like: 所以,理想情况下,我想说的是:
List<Publisher> publishers = new List<Publisher>();
publishers.DataSource = ods;
// -data manipulation goes here
gridPublishers.DataSource = publishers;
gridPublishers.DataBind();
But of course, a can't be DataBound, so... 但当然,一个不能是DataBound,所以......
Can anyone tell me how to populate my List of publishers, using the ObjectDataSource ? 谁能告诉我如何使用ObjectDataSource填充我的发布者列表?
I think you're going to have to do your data manipulation when fetching the records (inside GetPublishers() function) - this way you will still be able to use ObjectDataSource as your DataSource for the grid. 我认为在获取记录时(GetPublishers()函数内部)你将不得不进行数据操作 - 这样你仍然可以使用ObjectDataSource作为网格的数据源。
Alternatively you can try and populate the grid from your custom object (publishers) rather than using ObjectDataSource. 或者,您可以尝试从自定义对象(发布者)填充网格,而不是使用ObjectDataSource。 You will then have to implement function/s for the gridview paging (gridview_PageIndexChanging(object sender, GridViewPageEventArgs e) should do the job)
然后,您必须为gridview分页实现函数/(gridview_PageIndexChanging(对象发送者,GridViewPageEventArgs e)应该完成这项工作)
The simplest example of PageIndexChanging implementation (it's more of a trick than proper implementation really) is PageIndexChanging实现的最简单的例子(它比真正的正确实现更多的是一个技巧)
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
Although since you're dealing with large data and mentioned performance I'd recommend doing right and fetch only the rows you'll be displaying. 虽然既然你正在处理大数据和提到的性能,我建议你做正确的事情,只获取你将要显示的行。 The below can help
以下可以提供帮助
http://www.aspsnippets.com/Articles/Custom-Paging-in-ASP.Net-GridView-using-SQL-Server-Stored-Procedure.aspx http://www.aspsnippets.com/Articles/Custom-Paging-in-ASP.Net-GridView-using-SQL-Server-Stored-Procedure.aspx
http://www.4guysfromrolla.com/articles/031506-1.aspx http://www.4guysfromrolla.com/articles/031506-1.aspx
Have you tried using the Select() method? 您是否尝试过使用Select()方法?
List<Publisher> publishers = (List<Publisher>)ods.Select();
ObjectDataSource.Select Method
ObjectDataSource.Select方法
Retrieves data from the underlying data storage by calling the method that is identified by the SelectMethod property with the parameters in the SelectParameters collection.
通过使用SelectParameters集合中的参数调用SelectMethod属性标识的方法,从基础数据存储中检索数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.