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 …


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

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

Leave a reply to Varun Verma Cancel reply

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

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

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

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

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

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

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

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

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.