Linq to SQL custom paging

Discussion in 'ASP .Net' started by Mike, Nov 15, 2007.

  1. Mike

    Mike Guest

    Not sure if this is the right group. Please point me to alternatives if it
    isn't.

    I'm playing with the Linq To Sql designer in VWD Orcas, and am looking at
    how to apply custom paging to a GridView for display on a web page. I want
    to use <a href> for paging because I don't like the javascript links that
    the GridView employs with it's built-in paging. The following works:

    protected void Page_Load(object sender, EventArgs e)
    {
    int startRow = Convert.ToInt32(Request.QueryString["StartRow"]);
    PageProducts(startRow);
    }

    void PageProducts(int startRow)
    {
    NorthwindDataContext db = new NorthwindDataContext();

    var query = from p in db.Products
    where p.Order_Details.Count > 2
    select new
    {
    ID = p.ProductID,
    Name = p.ProductName,
    NumOrders = p.Order_Details.Count,
    Revenue = p.Order_Details.Sum(o => o.UnitPrice * o.Quantity)
    };

    int totalRecords = query.Count();
    int pageSize = 10;
    int totalPages = totalRecords/pageSize;
    if (totalRecords % 10 > 0)
    {
    totalPages += 1;
    }
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < totalPages; i++)
    {
    int pageNo = i + 1;
    sb.Append("<a href=\"Paging.aspx?StartRow=" + pageSize * i + "\">" +
    pageNo.ToString() + "</a>&nbsp;");
    }

    Literal1.Text = "Page: " + sb.ToString();

    GridView1.DataSource = query.Skip(startRow).Take(10);
    GridView1.DataBind();

    }

    But I am guessing that it causes two calls to the database: one to populate
    totalRecords, which gets all the records for calculating the paging html,
    and the second that retrieves just the records for display. Would I be
    right in my guess, and if so, is there a more efficient way of obtaining the
    count?
     
    Mike, Nov 15, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike N.
    Replies:
    0
    Views:
    516
    Mike N.
    Jul 31, 2008
  2. Mike Gleason jr Couturier

    LINQ to SQL and new MS SQL 2008 datatypes

    Mike Gleason jr Couturier, Oct 28, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    316
    Cowboy \(Gregory A. Beamer\)
    Oct 29, 2008
  3. Andy B

    Custom providers and linq to sql

    Andy B, Oct 31, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    293
    Andy B
    Oct 31, 2008
  4. George

    Linq or not Linq

    George, Nov 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    369
    Mike Gleason jr Couturier
    Nov 5, 2008
  5. Ryan Liu
    Replies:
    0
    Views:
    134
    Ryan Liu
    Dec 12, 2008
Loading...

Share This Page