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

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

Please 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: