Paging data retrieved from sharepoint list using PagedDataSource object

Standard

OverView

The good thing in data paging (from database) that you can retrieve only data  for a specific page the bad thing is you can’t do the same solution when you retrieve items from a SharePoint list.

for example if you have a GridView bounded from SharePoint list (through DataTable or whatever ) and you want to create a paging there is some ways to make it :

1-first you can allow paging into the GridView itself , the server will response the page with all data in ViewState then GridView will handle the clicks on pages number with post back , suppose you have 3000 items you will load the page with all data !!

2-second solution with SPQuery and SPListItemCollectionPosition objects this is a complex way , you will retrieve the List Item Collection in the server memory then start to specify the position and play with it to make your paging , the server will response with only the page data but this way not famous and its complex for the developers to implement as i saw , SharePoint out of the box lists web  parts using this way to create their paging .

3-using LINQ to objects , you can handle the LINQ query as a SQL query handle the paging , you will retrieve List Item Collection also in the server memory then with the query handling the paging ( SharePoint 2010 as i know today implemented the LINQ to SharePoint direct that’s means you can retrieve the data only for the page like sql paging , thanks to Ismael for this information 😀  )

4-using DataView you can retrieve your list items as a datatable then apply an expersion to the default view for this datatable that can handle the paging like SQL   for example

DataView.RowFilter =string.format (” Column >  {0}  and Column < {1} “,startPoint,endPoint);

and you will implement your code to change the startPoint and endPoint  variables everyTime the user clicks on paging controls

take a look here for more deitals about dataview expersions

http://www.csharp-examples.net/dataview-rowfilter/

its like a SQL queries paging !!

So !!   i found that the easiest way the last way with the data view but my solution will cover the handling of the query that will make paging with the PagedDataSource object.

The solution using PagedDataSource  (System.Web.UI.WebControls.PagedDataSource)

PagedDataSource object handle the query for paging , just it need to pass the data view of your data table and fill paging attributes of it then it will handle everything take a look and read the code comments

PagedDataSource pagedDS = new PagedDataSource();
pagedDS.AllowPaging = true;

//the default view of the data table retrived from GetDataTable() method from your ListItemCollection object
pagedDS.DataSource = listItemsCollections.GetDataTable().DefaultView;

//the page size – the number of data that you need to view into the page
pagedDS.PageSize = _pageSize;

//the current page index number
pagedDS.CurrentPageIndex = _currentPage;

//pass your paged data source object to your GridView as a data source

yourGridView.DataSource = pagedDS;
yourGridView.DataBind();

just do this and PagedDataSource object will handle the query and everything

and you can specify the pages size and page index in the events of your paging controls , for example you have next and previous link

take a look into the complete code  and please read the comments to understand deeply

//set the number of data per page

private int _pageSize = 20;

//this variable to
private int _currentPage;

protected void Page_Load(object sender, EventArgs e)
{

//view states for paging set the current page index

if (ViewState[“currentPage”] == null) {  ViewState[“currentPage”] = 0;  }

//set the current page variable from the view state of it (filled when the user clicks on next and previous links take a //look to next and previous links events below )
_currentPage = (int)ViewState[“currentPage”];

if (!IsPostBack)
{
getItems();
}

private void GetItems()

{

PagedDataSource pagedDS = new PagedDataSource();
pagedDS.AllowPaging = true;

//get your list item collection

SPListItemCollection listItemsCollections= [write here your method to ger list item collection object  from your list]

//the default view of the data table retrived from GetDataTable() method from your ListItemCollection object
pagedDS.DataSource = listItemsCollections.GetDataTable().DefaultView;

//the page size – the number of data that you need to view into the page
pagedDS.PageSize = _pageSize;

//the current page index number
pagedDS.CurrentPageIndex = _currentPage;

//enable the next and previous  links  when the number of  items is greater than your page number and disable if not , also the previous based on IsLastPage and IsFirstPage attributes in PagedDataSource object , what it easy !!!

_lbtnNext.Enabled = !pagedDS.IsLastPage;
_lbtnPrev.Enabled = !pagedDS.IsFirstPage;

//pass your paged data source object to your GridView as a data source

yourGridView.DataSource = pagedDS;
yourGridView.DataBind();

}

//event handler for next button
protected void lbtnNext_Click(object sender, EventArgs e)
{

//increase the current page variable everytime the user clicks on the next link
_currentPage++;

//save it to view state for the next request
ViewState[“currentPage”] = _currentPage;

//bind data
getMySites();
}

//event handler for previous button
protected void lbtnPrev_Click(object sender, EventArgs e)
{

//check if the current page is not the first page to decrease the current page number
if (_currentPage > 0)
_currentPage–;
else
_currentPage = 0;
////save it to view state for the next request
ViewState[“currentPage”] = _currentPage;
//bind data

getMySites();
}

hope it will help

Thanks

Advertisements

4 thoughts on “Paging data retrieved from sharepoint list using PagedDataSource object

  1. Pingback: ListPager<T> using SPListItemCollectionPosition « Hernán Veiras

  2. that’s a nice article. Can you shed some light here, i have also posted it on Sharepoint forum :

    http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010programming/thread/40bd7bd7-8408-43a2-8153-f483a2a71a5e

    am having issue with Paging in gridview control. I have 1000 rows in the database and i want to show only 30 at a time. Below are the properties that i set. The issue is that the page numbers disapper after a post back like clicking a button. It works perfectly fine when i click on the other page numbers i.e when the page loads with data form that page number it still have page numbers.

    a. GridView control is using a LInq datasource.

    b. i have the following in the aspx page in the grid declaration

    AllowPaging=”true” PageSize=”30″ OnPageIndexChanging =”SPGActivites_PageIndexChanging”

    c. this is what i am specifying in Pageload code

    SPGActivites.PagerTemplate = null;

    SPGActivites.PagerSettings.Position = PagerPosition.TopAndBottom;

    SPGActivites.PagerSettings.Mode = PagerButtons.NumericFirstLast;

    SPGActivites.PageIndexChanging += new GridViewPageEventHandler(SPGActivites_PageIndexChanging);

    SPGActivites.PagerTemplate = null;

    d. in PageIndexChanging

    protected void SPGActivites_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    SPGActivites.PageIndex = e.NewPageIndex;

    SPGActivites.PagerTemplate = null;

    SPGActivites.PagerSettings.Position = PagerPosition.TopAndBottom;

    SPGActivites.PagerSettings.Mode = PagerButtons.NumericFirstLast;

    SPGActivites.PageIndexChanging += new GridViewPageEventHandler(SPGActivites_PageIndexChanging);

    SPGActivites.PagerTemplate = null;

    SPGActivites.DataBind();

    }

    The page numbers appear if i rebing the data in the post back even like in the button code, but the question is why do i have to rebind data if i am just clikcing a button.

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s