Question

J'ai créé un service BCS et a créé une liste externe du BCS contenu de type. Je l'ai ensuite essayé d'ajouter un contrôle SPGridView à un webpart. Je reçois une exception, dès que j'appelle la méthode SPGridview de mon DataBind(), voici ce que l'apparence de code comme:

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

}

La méthode DataBind() jette l'exception suivante:

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)

J'ai vérifié que this.we_list est pas vide (Dans l'onglet locals de débogueur Visual Studio, je peux voir this.we_list.Items.Count est réglé sur 99, bien que this.we_list.ItemCount est réglé sur 0.)

En outre, que tout semble fonctionner bien contre les listes non externes, mais je ne vois rien dans la documentation sur les listes externes ne sont pas pris en charge dans SPGridView ou SPDataSource, et l'exception ne fait aucune mention des listes externes ne sont pas pris en charge. A terme quiconque dans cette question?

Était-ce utile?

La solution

Cela semble être un possible bug SharePoint Server 2010 (j'utilise Sharepoint Server 2010 Enterprise Edition). En fin de compte, je résolu le problème en ajoutant une méthode de conversion de to_datatable () à mon entité de service BCS qui utilise simplement la statis méthode Readlist (), recueille sa sortie, et insère les données dans un objet DataTable.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top