Question

I have a GridView that I'm binding from my database table "Publishers". The "Publishers" table has 22,000+ rows inside it and every bind event is taking a LONG time.

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

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

Can anyone tell me how to populate my List of publishers, using the ObjectDataSource ?

No correct solution

OTHER TIPS

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.

Alternatively you can try and populate the grid from your custom object (publishers) rather than using ObjectDataSource. You will then have to implement function/s for the gridview paging (gridview_PageIndexChanging(object sender, GridViewPageEventArgs e) should do the job)

The simplest example of PageIndexChanging implementation (it's more of a trick than proper implementation really) is

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

https://web.archive.org/web/20211020140032/https://www.4guysfromrolla.com/articles/031506-1.aspx

Have you tried using the Select() method?

List<Publisher> publishers = (List<Publisher>)ods.Select();

ObjectDataSource.Select Method

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top