Sunday 2 March 2014

Bind Sharepoint list to GridView

Bind Sharepoint list to GridView

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;
namespace SPListGridView.Layouts.SPListGridView
{
public partial class ApplicationPage1 : LayoutsPageBase
{
// refer to your site collection
SPSite mySite = new SPSite(@"http://jiteng:42204");
// create class level spweb and splist object
SPWeb myWeb;
SPList myList;
protected void Page_Load(object sender, EventArgs e)
{
myWeb = mySite.OpenWeb();
myList = myWeb.Lists["ListTimerJob"];
if (!Page.IsPostBack)
{
BindToGrid(myList, grdView);
}
}
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("Title", typeof(string));
//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["Title"] = result["Title"].ToString();
//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 = "Title";
boundField.DataField = "Title";
gridView.Columns.Add(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();
}
}
}
==================================
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;
namespace SPListGridView.Layouts.SPListGridView
{
public partial class ApplicationPage1 : LayoutsPageBase
{
// refer to your site collection
SPSite mySite = new SPSite(@"http://jiteng:42204");
// create class level spweb and splist object
SPWeb myWeb;
SPList myList;
protected void Page_Load(object sender, EventArgs e)
{
myWeb = mySite.OpenWeb();
myList = myWeb.Lists["ListTimerJob"];
if (!Page.IsPostBack)
{
BindToGrid(myList, grdView);
}
}
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("Title", typeof(string));
// Create rows for each splistitem
DataRow row;
foreach (SPListItem result in results)
{
row = table.Rows.Add();
row["Title"] = result["Title"].ToString();
}
// create the bound fields
SPBoundField boundField;
boundField = new SPBoundField();
boundField.HeaderText = "Title";
boundField.DataField = "Title";
gridView.Columns.Add(boundField);
gridView.AutoGenerateColumns = false;
gridView.DataSource = table.DefaultView;
gridView.DataBind();
}
}
}