Using SPGridView to bound to list data in SharePoint

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&#8243;);

    // 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 …


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

22 thoughts on “Using SPGridView to bound to list data in SharePoint”

  1. 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);

    Like

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

    Like

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

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s