Thursday, September 18, 2008

Using the SPGridView in custom code


<%@ Assembly Name="Microsoft.SharePoint, Version=,
Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Assembly Name="CustomApplicationPages, Version=,
Culture=neutral, PublicKeyToken=d4e5777b16a5749f" %>

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master"
Inherits="CustomApplicationPages.ApplicationPage" EnableViewState="false" EnableViewStateMac="false" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint,Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">

<SharePoint:SPGridView runat="server"
AlternatingRowStyle-BackColor="#EEEEEE" />

<asp:Content ID="PageTitle" contentplaceholderid="PlaceHolderPageTitle" runat="server">SharePoint GridView</asp:Content>

<asp:Content ID="PageTitleInTitleArea" contentplaceholderid="PlaceHolderPageTitleInTitleArea" runat="server">
Application Page : Using the SPGridView control
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CustomApplicationPages {

public class ApplicationPage : LayoutsPageBase {

// add control fields to match controls tags on .aspx page
protected SPGridView grid;

protected override void OnLoad(EventArgs e) {

grid = new SPGridView();
grid.AutoGenerateColumns = false ;
grid.CssClass = "ms-listviewtable";
grid.AlternatingRowStyle.CssClass = "ms-alternating";
grid.Width = new Unit(50, UnitType.Percentage);
grid.GridLines = GridLines.None;
grid.HeaderStyle.Font.Bold = true;
grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

DataTable table = new DataTable();
//TO DO : Populate the DataTable here <-----

SPBoundField col= new SPBoundField();
col.HeaderText = "Product Name";
col.DataField = "ProductName";


grid.DataSource = table.DefaultView;



Anonymous,  October 16, 2012 at 6:33 AM  

This code did not work for me, even after adding the missing reference, the sharepoint application page fails to load.

Sandeep Nahta October 16, 2012 at 7:16 AM  

I think you are using different datatable with diff columns.. try to add break point around DataBind - it might be broken around that line of code

Anonymous,  February 19, 2013 at 8:04 AM  

Can u plz help wid using Sorting and Filtering features of SPGridView?

Sandeep Nahta February 19, 2013 at 8:05 AM  

Check out

Dinesh November 4, 2014 at 10:17 AM  

Hallo Sandeep,

Editing is working but adding new item using the Gridview is not working. could you help me
do that ?..