These are the few points we need to remember while using SPGridView
With SPGridView we would inherit the same look and feel as the rest of SharePoint site because it makes use of the same CSS classes that the other grids in SharePoint use.
We need to set AutoGenerateColumns=false and explicitly bind the columns.
Create a new asp.net page
Put the following directive to use SPGridView
<%@ Register TagPrefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls”
Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %
Declare the control
<SharePoint:SPGridView runat=”server” ID=”grdView”
AutoGenerateColumns=”false” />
Declaring class level variable
public partial class _Default : System.Web.UI.Page
{
// refer to your site collection
SPSite mySite = new SPSite(@”http://d-1246:100″);
// create class level spweb and splist object
SPWeb myWeb;
SPList myList;
Code for Page_Load EventHandler
protected void Page_Load(object sender, EventArgs e)
{
myWeb = mySite.OpenWeb();
myList = myWeb.Lists["ListName"];
if (!Page.IsPostBack)
{
BindToGrid(myList, grdPropertyValues);
}
}
Code for BindToGrid method
private void BindToGrid(SPList myList, SPGridView gridView)
{
//grdView.Columns.Clear();
// get all the listitem
SPListItemCollection results = myList.Items;
// create the datatable object
DataTable table;
table = new DataTable();
table.Columns.Add(“Type”, typeof(string));
table.Columns.Add(“Name”, typeof(string));
table.Columns.Add(“Created”, typeof(string));
// Create rows for each splistitem
DataRow row;
foreach (SPListItem result in results)
{
row = table.Rows.Add();
row["Type"] = result["Type"].ToString();
row["Name"] = result["Name"].ToString();
row["Created"] = result["Created"].ToString();
}
// create the bound fields
SPBoundField boundField;
boundField = new SPBoundField();
boundField.HeaderText = “Type”;
boundField.DataField = “Type”;
boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
boundField.ItemStyle.Wrap = false;
gridView.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = “Name”;
boundField.DataField = “Name”;
gridView.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = “Created”;
boundField.DataField = “Created”;
gridView.Columns.Add(boundField);
gridView.AutoGenerateColumns = false;
gridView.DataSource = table.DefaultView;
gridView.DataBind();
}
That’s it …


But how to do edit , update and delete
its very good
thanks to author,
realy good post – short and clear
Very Nice Post , Thanks for the help
Was trying out this code. Not sure what context you would instantiate this or to get the actual grid to show, but need to add this line at the end in fucntion BindToGrid:
Controls.Add(gridView);
You don’t have to iterate through all your data. You can just use GetSiteData.
Here you can find an example.
Hi All
http://www.fewlines4biju.com/Home.aspx?TagID=16
Follow for some sharepoint tutorials and Interview questions
Nice post, tnks
very helpful
Superb and clean work
cool man.i cud understand the concept of SPGridview just thru this blog.Thanks.Nice example.
I missed the test.ascx file in previous post.
Let me know if u can figure out the mistake.
I am pretty new to sharepoint and asp as well. I was trying to get the sharepoint list data into gridview. I did your code above to bind the gridview to sharepoint list, but it was working.
I have followed your procedure.
I had only couple of feilds, Requested By and Topic. I replaced them and the sharepoint site from ur code. It shows an error.
Here the test.ascx.cs file .
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
public partial class Test : System.Web.UI.UserControl
{
//refer to your site collection
SPSite mySite = new SPSite(@”http://sirishac:2086″);
// create class level spweb and splist object
SPWeb myWeb;
SPList myList;
SPGridView gridView;
protected void Page_Load(object sender, EventArgs e)
{
myWeb = mySite.OpenWeb();
myList = myWeb.Lists["Requests"];
if (!Page.IsPostBack)
{
BindToGrid(myList, gridView);
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void BindToGrid(SPList myList, SPGridView gridView)
{
//grdView.Columns.Clear();
// get all the listitem
SPListItemCollection results = myList.Items;
// create the datatable object
DataTable table;
table = new DataTable();
table.Columns.Add(“Requested By”, typeof(string));
table.Columns.Add(“Topic”, typeof(string));
// Create rows for each splistitem
DataRow row;
foreach (SPListItem result in results)
{
row = table.Rows.Add();
row["Requested By"] = result["Requested By"].ToString();
row["Topic"] = result["Topic"].ToString();
}
// create the bound fields
SPBoundField boundField;
boundField = new SPBoundField();
boundField.HeaderText = “Requested By”;
boundField.DataField = “Requested By”;
//boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
//boundField.ItemStyle.Wrap = false;
gridView.Columns.Add(boundField);
boundField = new SPBoundField();
boundField.HeaderText = “Topic”;
boundField.DataField = “Topic”;
gridView.Columns.Add(boundField);
gridView.AutoGenerateColumns = false;
gridView.DataSource = table.DefaultView;
gridView.DataBind();
}
}
Here is the test.ascx file:
If you are able to figure out the fault,please let me know..
Hi, I am trying to accomplish the same thing but my SPListCollection contains Task items and I would like to display some Task-specific fields such as Status, and Assigned To.
However when I try to do this using code very similar to your own, I am met with the following error:
“A field or property with the name ‘Status’ was not found on the selected data source”
Do you have any idea how I can get the SPGridView to recognise the Task-specific fields?
–Stephen
There is a much better way to do this by using SPGridView in combination with SPDataSource:
http://www.sharepointnutsandbolts.com/2008/06/spdatasource-every-sharepoint-developer.html
http://www.sharepointnutsandbolts.com/2008/07/spdatasource-every-developer-friend.html
[...] Using SPGridView to bound to list data in SharePoint [...]