Thursday, December 9, 2010

GridView

Bind Gridview with Sharepoint list data
Step 1. Create ASPX page, and add following code
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
asp:GridView
AutoGenerateColumns is false

Step 2. Add reference:- Microsoft.Sharepoint

Step 3.Add following code into ASPX.cs file
Using Microsoft.Sharepoint;

SPSite oSite= new SPSite("SiteUrl");
SPWeb oWeb;
SPList oList;
DataTable dt;

Page_Load()
{
if(!Page.IsPostBack)
{
FetchData();
}
}

Protected void FetchData()
{
oWeb=oSite.OpenWeb();
oList=oWeb.Lists["ListName"];
SPListItemCollection oListCol;
oListCol=oList.Items;
dt=new DataTable ();
dt.Columns.Add("Title",typeof(string));
dt.Columns.Add("Created",typeof(DateTime));
DataRow dr;
foreach(SPListItem oListItem in oListCol)
{
dr=dt.Rows.Add();
dr["Title"]=oListItem["Title"].tostring();
}

gridview1.Datasource=dt;
gridview1.DataBind();
}

No comments:

Post a Comment