Thursday, September 18, 2008

Using the SPGridView in custom code

ApplicationPage.aspx


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

<%@ Assembly Name="CustomApplicationPages, Version=1.0.0.0,
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=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

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

<SharePoint:SPGridView runat="server"
ID="grid"
AutoGenerateColumns="false"
RowStyle-BackColor="#DDDDDD"
AlternatingRowStyle-BackColor="#EEEEEE" />
</asp:Content>

<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
</asp:Content>
ApplicationPage.cs
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.Columns.Add(col);
Controls.Add(grid);

grid.DataSource = table.DefaultView;
grid.DataBind();

}
}
}

5 comments:

Anonymous,  October 16, 2012 at 6:33 AM  

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

Sandeep 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 February 19, 2013 at 8:05 AM  

Check out http://www.codeproject.com/Articles/35536/Creation-of-a-SPGridview-Webpart-having-Pagination

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 ?..