Pergunta

Criei um serviço BCS e criei uma lista externa do tipo de conteúdo do BCS. Eu então tentei adicionar um SPGridView Controle para uma parte da web. Estou recebendo uma exceção assim que ligar para o meu SPGridview's DataBind() Método, aqui está como é o código:

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

}

o DataBind() O método está lançando a seguinte exceção:

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)

Eu verifiquei isso this.we_list não está vazio (no Visual Studio Debugger's locals guia, eu posso ver this.we_list.Items.Count está definido como 99, embora this.we_list.ItemCount está definido como 0.)

Além disso, tudo parece funcionar bem contra listas não extras, mas não vejo nada nos documentos sobre listas externas não serem apoiadas em SPGridView ou SPDataSource, e a exceção não menciona listas externas que não estão sendo suportadas. Alguém encontrou esse problema?

Foi útil?

Solução

Este parece ser um possível bug do SharePoint Server 2010 (estou usando o SharePoint Server 2010 Enterprise Edition). Por fim, resolvi o problema adicionando um método de conversão TO_DATATABLE () à minha entidade de serviço BCS que simplesmente usa o método statis readlist (), coleta sua saída e insere os dados em um objeto datatable.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top