web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Using SPGridView to bound t...

Using SPGridView to bound to list data in SharePoint

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee
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 …

Posted in MOSS, SharePoint Tagged: SharePoint

This was originally posted here.

Comments

*This post is locked for comments