Paging error with SPGridView

I am using the OOTB SPGridViewPager control associated with SPGridView. This worked fine in 2007, but now we updated to 2010, I get the following error when trying to page through a dataset;

System.InvalidCastException: Cannot cast an object of type "System.Int32" to type "System.String". at Microsoft.SharePoint.WebControls.SPGridView.set_PageIndex (Int32 value) at Microsoft.SharePoint.WebControls.SPGridViewPager.OnClickNext (EventArgs arguments) at Microsoft.SharePoint.WebControls.SPGridViewPager.RaisePostBackEvent (String eventArgument) in System .RaisePostBackEvent (IPostBackEventHandler sourceControl, String eventArgument) in System.Web.UI.Page.ProcessRequestMain (Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint

My code still refers to the Microsoft.SharePoint version 12 build, so I'm not quite sure what has changed to cause the problem.

+4
source share
2 answers

Same problem. However, I do not use the SPGridViewPager control. I just call .PageIndex = e.NewPageIndex; Both are of type Integer. I am doing the assignment in the PageIndexChanging(object sender, GridViewPageEventArgs e) method PageIndexChanging(object sender, GridViewPageEventArgs e) .

Not sure what is happening at this moment. Now I look at it in Reflector.

I was wondering if you are using DataKeys? It looks like there are 2 throws. See below.

 public override void set_PageIndex(int value) { int pageIndex = this.PageIndex; DataKeyArray dataKeys = this.DataKeys; base.PageIndex = value; if (pageIndex != this.PageIndex) { this.PreviousPageIndex = pageIndex; if ((dataKeys == null) || (dataKeys.Count <= 0)) { this.PreviousPageFirstRowDataKey = null; this.PreviousPageLastRowDataKey = null; } else { **this.PreviousPageFirstRowDataKey = (string) dataKeys[0].Value; this.PreviousPageLastRowDataKey = (string) dataKeys[dataKeys.Count - 1].Value;** } } } 

To fix this, I had to study my data source and datakeys more closely. I have a set of records returned from SQL Server, and what I do is bind them to POCO. This class had several public properties of type Integer. These integers were my datakeys on the grid. Instead, I replaced their type with a string to get around the casting issue.

Hope this makes sense. He fixed it for me.

+4
source

Wow, this seems like a bug in the SPGridViewPager. I just ran into the same problem and changing the type of my data key from int to string, solved the problem. Thanks for the comment, it really helped me!

0
source

All Articles