sharepoint webpart 后台绑定spgridview

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;

namespace Domcool.AbcBankPortal1.SearchCenter
{
    [Guid("0f0d4bed-ad56-49b3-baf0-7c3ad9483255")]
    public class GetNews : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null;
        private SPGridView sPGridView;
        private DataView dataView;


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }


        public GetNews()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();
                    BindSPGridView();//给SPGridView绑定数据
                    this.Controls.Add(sPGridView);//添加SPGridView
                    // Your code here...
                    //this.Controls.Add(new LiteralControl(this.MyProperty));
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();
                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
        /// <summary>
        /// 给SPGridView绑定数据
        /// </summary>
        private void BindSPGridView()
        {
            sPGridView = new SPGridView();
            sPGridView.DataSource = GetDetail();//给SPGridView设置数据源
            sPGridView.AutoGenerateColumns = false;
            AddDetail(sPGridView);//给SPGridView添加显示内容
            sPGridView.DataBind();
        }
        /// <summary>
        /// 给SPGridView添加显示内容
        /// </summary>
        /// <param name="sPGridView"></param>
        private void AddDetail(SPGridView sPGridView)
        {
            //添加普通绑定列
            BoundField colId = new BoundField();
            colId.DataField = "ID";
            colId.HeaderText = "ID";
            sPGridView.Columns.Add(colId);

            //添加普通绑定列
            BoundField colTitle = new BoundField();
            colTitle.DataField = "Title";
            colTitle.HeaderText = "标题";
            sPGridView.Columns.Add(colTitle);

            //添加普通绑定列
            BoundField colContent = new BoundField();
            colContent.DataField = "Content";
            colContent.HeaderText = "内容";
            sPGridView.Columns.Add(colContent);
        }
        /// <summary>
        /// 将外网搜索结果信息和DataView绑定
        /// </summary>
        private DataView GetDetail()
        {
            dataView = new DataView();
            //获取列表
            SPWeb sPWeb = SPContext.Current.Web;
            SPList sPList = sPWeb.Lists["外网搜索结果"];

            if (sPList.Items != null && sPList.Items.Count != 0)
            {
                dataView = sPList.Items.GetDataTable().DefaultView;
            }
            return dataView;
        }
    }
}

你可能感兴趣的:(sharepoint webpart 后台绑定spgridview)