在Sharepoint 2010中将外部(BCS)列表绑定到SPDataGrid

在Sharepoint 2010中将外部(BCS)列表绑定到SPDataGrid,sharepoint,sharepoint-2010,spgridview,bcs,Sharepoint,Sharepoint 2010,Spgridview,Bcs,我已经创建了一个BCS服务,并根据BCS内容类型创建了一个外部列表。 然后,我尝试将SPGridView控件添加到Web部件。调用SPGridview的DataBind方法时,我会收到一个异常,代码如下所示: namespace BCSService.CustomWebPart { [ToolboxItemAttribute(false)] public class CustomWebPart : WebPart { // Visual Studio mi

我已经创建了一个BCS服务,并根据BCS内容类型创建了一个外部列表。 然后,我尝试将SPGridView控件添加到Web部件。调用SPGridview的DataBind方法时,我会收到一个异常,代码如下所示:

namespace BCSService.CustomWebPart
{
    [ToolboxItemAttribute(false)]
    public class CustomWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/BCSShims/CustomWorkEstimateWebPart/CustomWorkEstimateWebPartUserControl.ascx";
        private SPGridView gv;
        private SPDataSource spdata;
        private SPSite site;
        private SPWeb web;
        private SPList we_list;


    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);

        this.site = SPContext.Current.Site;
        this.web = this.site.OpenWeb();
        this.we_list = this.web.Lists["BCSList"];

        this.spdata = new SPDataSource();
        Controls.Add(this.spdata);

        this.gv = new SPGridView();
        this.gv.AutoGenerateColumns = false;

        Controls.Add(this.gv);            
    }

    protected void BindColumns()
    {
        this.spdata.DataSourceMode = SPDataSourceMode.List;
        this.spdata.List = this.we_list;
        this.spdata.UseInternalName = true;
        this.spdata.DataBind();

        this.gv.AllowSorting = false;
        this.gv.PageSize = 200;
        this.gv.DataSource = this.spdata;

        Dictionary<string, string> listFields = new Dictionary<string, string>();
        listFields.Add("CompanyName", "Company Name");
        listFields.Add("ContactDetails", "Contact Details");
        listFields.Add("ProjectDescription", "Description");

        foreach (var row in listFields)
        {
            SPBoundField boundField = new SPBoundField();
            boundField.HeaderText = row.Value;
            boundField.DataField = row.Key;
            this.gv.Columns.Add(boundField);
        }
    }


    protected override void RenderContents(HtmlTextWriter writer)
    {

        if (!Page.IsPostBack)
        {
            this.BindColumns();
            this.gv.DataBind();
        }

        this.gv.RenderControl(writer);
    }
}
我已经在Visual Studio调试器的“局部变量”选项卡中验证了this.we_列表不是空的,我可以看到this.we_list.Items.Count设置为99,但this.we_list.ItemCount设置为0


此外,所有这些似乎都适用于非外部列表,但我在文档中没有看到任何关于SPGridView或SPDataSource不支持外部列表的内容,并且异常没有提到不支持外部列表。有人遇到过这个问题吗?

这可能是Sharepoint Server 2010的一个bug,我正在使用Sharepoint Server 2010企业版。最终,我通过向我的BCS服务实体添加一个to_datatable转换方法解决了这个问题,该实体只使用statis ReadList方法,收集其输出,并将数据插入datatable对象

Object reference not set to an instance of an object.
   at Microsoft.SharePoint.WebControls.SPDataSourceViewResultItem.System.ComponentModel.ICustomTypeDescriptor.GetProperties()
   at System.ComponentModel.TypeDescriptor.MergedTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetProperties()
   at System.ComponentModel.TypeDescriptor.GetPropertiesImpl(Object component, Attribute[] attributes, Boolean noCustomTypeDesc, Boolean noAttributes)
   at System.ComponentModel.TypeDescriptor.GetProperties(Object component)
   at Microsoft.SharePoint.WebControls.SPBoundField.DataBindingEventHandler(Object sender, EventArgs e)
   at System.Web.UI.Control.OnDataBinding(EventArgs e)
   at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
   at System.Web.UI.Control.DataBindChildren()
   at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
   at System.Web.UI.Control.DataBindChildren()
   at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
   at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
   at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
   at Microsoft.SharePoint.WebControls.SPGridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
   at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
   at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
   at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
   at BCSService.CustomWebPart.CustomWorkEstimateWebPart.RenderContents(HtmlTextWriter writer)