使用SPGridView分页错误

时间:2010-08-02 09:20:20

标签: sharepoint-2010

我正在使用与SPGridView关联的OOTB SPGridViewPager控件。这在2007年工作正常,但现在我们已升级到2010年,在尝试浏览数据集时出现以下错误;

  

System.InvalidCastException:无法将类型为“System.Int32”的对象强制转换为“System.String”。在System.Web.UI.Page上的Microsoft.SharePoint.WebControls.SPGridViewPager.OnClickNext(EventArgs args)的Microsoft.SharePoint.WebControls.SPGridView.set_PageIndex(Int32值)处于Microsoft.SharePoint.WebControls.SPGridViewPager.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.ProcessRequestMain中的.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint

我的代码仍然指的是版本12的Microsoft.SharePoint程序集,所以我不太确定导致问题的原因是什么。

2 个答案:

答案 0 :(得分:4)

这里的问题相同。但是,我没有使用SPGridViewPager控件。我只是调用.PageIndex = e.NewPageIndex;两者都是Integer类型。我正在使用PageIndexChanging(object sender, GridViewPageEventArgs e)方法进行作业。

目前还不确定发生了什么。我现在正在Reflector中看这个。

我想知道你是否在使用DataKeys?看起来这里发生了2次演员表演。见下文。

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;**
        }
    }
}

要解决这个问题,我必须仔细查看我的数据源和数据键。我有一组从SQL Server返回的记录,我正在做的是将它们绑定到POCO。这个类有几个Integer类型的公共属性。这些整数是网格上的数据键。我用字符串替换了他们的类型,而不是绕过铸造问题。

希望这是有道理的。它为我修好了。

答案 1 :(得分:0)

哇,好像这是SPGridViewPager中的一个错误。我刚刚面临同样的问题,我的数据键的类型从int更改为字符串解决了问题。感谢您的评论,这对我很有帮助!

相关问题