View Single Post
  #7 (permalink)  
Old March 21st, 2008, 10:52 AM
dparsons dparsons is offline
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Paging is quite simple using LINQ, a basic method might be something like this:

private void BindDataSource(int rowNumber)
{
TBHDataContext ctx = new TBHDataContext();
var articles = from a in ctx.Articles
                 where a.ArticleID >= 1;
gv.DataSource = articles.Skip(rowNumber).Take(15);
gv.DataBind();
}

The Skip() and Take() methods is where the magic happens. Take(15) returns the first 15 records starting from the Row position defined in Skip(). So if rowNumber = 1 then Take(15) would return the first 15 records in your result set.

hth.

[edit]: The obvious question might be: "Thats great but how do I know what to skip by?" In the case of the GridView if you handle the PageIndexChanging event you could do something like this:

protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
BindDataSource(GetNextPage(e.NewPageIndex , gv.PageSize));
}

private int GetNextPage(int pageIndex, int pageSize)
{
if(pageIndex == 1){ return 1; }
else
{
     return ((pageIndex * pageSize) - (pageSize - 1));
}
}

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========