Search This Blog

Thursday, March 12, 2015

SPGridView and SPMenuField: Displaying custom data through SharePoint lists



Create a Web Part
Any blank Web Part will do - you could use the template provided by Visual Studio if you have the SharePoint SDK installed.  My web part started out life like this:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
 
namespace ListMenuSample
{
    public class ListMenuSample : System.Web.UI.WebControls.WebParts.WebPart
    {
        private SPGridView oGrid;
        private TVData oDataset;
        private DataView oView;
 
        private void PopulateDataset()
        {
            // TODO
        }
 
        protected override void CreateChildControls()
        {
            // TODO
        }
 
        void oGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            // TODO
        }
 
    }
}


private void PopulateDataset()
        {
            oDataset = new TVData();
            oDataset.Presenters.AddPresentersRow(1, "Jeremy Paxman", "Newsnight");
            oDataset.Presenters.AddPresentersRow(2, "Kirsty Wark", "Newsnight");
            oDataset.Presenters.AddPresentersRow(6, "Bill Turnbull", "Breakfast");
            oDataset.Presenters.AddPresentersRow(7, "Sian Williams", "Breakfast");
            // plus a few more entries
        }
 

protected override void CreateChildControls()
        {
            PopulateDataset();
            oView = new DataView(oDataset.Presenters);

            oGrid = new SPGridView();
            oGrid.DataSource = oView;
            oGrid.AutoGenerateColumns = false;
            oGrid.AllowSorting = true;
            oGrid.Sorting += new GridViewSortEventHandler(oGrid_Sorting);

            BoundField colName = new BoundField();
            colName.DataField = "PresenterName";
            colName.HeaderText = "Presenter Name";
            colName.SortExpression = "PresenterName";
            oGrid.Columns.Add(colName);

            // Add the menu control here

            BoundField colProgramme = new BoundField();
            colProgramme.DataField = "ProgrammeName";
            colProgramme.HeaderText = "Programme";
            colProgramme.SortExpression = "ProgrammeName";
            oGrid.Columns.Add(colProgramme);
           
            Controls.Add(oGrid);
            oGrid.DataBind();

            base.CreateChildControls();
        }

void oGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            string lastExpression = "";
            if (ViewState["SortExpression"] != null)
                lastExpression = ViewState["SortExpression"].ToString();

            string lastDirection = "asc";
            if (ViewState["SortDirection"] != null)
                lastDirection = ViewState["SortDirection"].ToString();

            string newDirection = "asc";
            if (e.SortExpression == lastExpression)
                newDirection = (lastDirection == "asc") ? "desc" : "asc";

            ViewState["SortExpression"] = e.SortExpression;
            ViewState["SortDirection"] = newDirection;

            oView.Sort = e.SortExpression + " " + newDirection;
            oGrid.DataBind();
        }



Refernces



http://blogs.msdn.com/b/powlo/archive/2007/02/25/displaying-custom-data-through-sharepoint-lists-using-spgridview-and-spmenufield.aspx

No comments:

Post a Comment