SPGridView: Paging does not work

The SPGridView control supports the cascading style sheets that are built into Microsoft SharePoint Foundation 2010. You can present data in a grid view that closely resembles the view that SharePoint Foundation 2010 uses to display data from lists. You can also sort and group rows.

This class does not support auto-generated columns. This means that you must explicitly bind each column using SPBoundField.

There are a lot of tutorials on this amazing UX control, so I won’t go into detail on how to implement it. A nice reference is the post on msdn blog by Paul Robinson.

However, I came across an issue by enabling the paging functionality. Whilst playing around with the SPGridViewPager, I had to bind it to my SPGridView control:

SPGridViewPager pager = new SPGridViewPager();
pager.GridViewId = oGrid.ID;
this.Controls.Add(pager);

So far so good. All I had to do now was assign an ID to my SPGridView:

oGrid.ID = Guid.NewGuid().ToString();

Seems legit, no? Apparently, your control’s ID is overwritten every time your web part is loaded, aswell on postbacks:

Postback:

As a solution you can use ViewState to track your ID or use an oldschool static one. When the webpart has it’s correct ID, the paging will trigger the correct page of the DataSet. More info on the lifecycle of a SharePoint webpart can be found here.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s