开发者

Paging error using SPGridView

开发者 https://www.devze.com 2023-01-09 15:45 出处:网络
I\'m using the OOTB SPGridViewPager control associated with an SPGridVi开发者_Go百科ew. This worked fine in 2007 but now we\'ve upgraded to 2010, I get the following error when trying to page through

I'm using the OOTB SPGridViewPager control associated with an SPGridVi开发者_Go百科ew. This worked fine in 2007 but now we've upgraded to 2010, I get the following error when trying to page through the dataset;

System.InvalidCastException: Unable to cast 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 args) at Microsoft.SharePoint.WebControls.SPGridViewPager.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint

My code is still referring to the version 12 Microsoft.SharePoint assembly so I'm not quite sure what's changed to cause the issue.


Same issue here. I am not using the SPGridViewPager control however. I'm simply calling .PageIndex = e.NewPageIndex; Both are of type Integer. I am making the assignment in the PageIndexChanging(object sender, GridViewPageEventArgs e) method.

Not sure what's going on at this point. I'm looking at this in Reflector at the moment.

I was wondering if you're using DataKeys? Looks like there are 2 casts happening here. 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 take a closer look at my datasource and datakeys. I have a set of records that are returned from SQL Server and what I was doing is binding them to a POCO. This class had several public properties of type Integer. These Integers were my datakeys on the grid. I replaced their type with a string instead to bypass the casting issue.

Hope this makes sense. It fixed it for me.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消