Bind SPLIst to Grid in Sharepoint 2013
private void GetCorporateOverview()
{
using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oweb = osite.OpenWeb())
{
string currentYear = DateTime.Now.Year.ToString();
int year = Convert.ToInt32(currentYear);
string yearvalue = Convert.ToString(year - 1);
SPList olist = oweb.Lists.TryGetList("CorporateOverview");
SPQuery querry = new SPQuery();
string query = string.Format(@"<Where><Eq><FieldRef Name='FiscalYear'/><Value Type='Lookup'>{0}</Value></Eq></Where>", yearvalue);
querry.Query = query;
SPListItemCollection collListItems = olist.GetItems(querry);
DataTable dt = new DataTable();
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Description", typeof(string));
foreach (SPListItem item in collListItems)
{
DataRow dr = dt.NewRow();
dr["Title"] = item["Title"] != null ? item["Title"].ToString() : string.Empty;
dr["Description"] = item["Description"] != null ? item["Description"].ToString() : string.Empty;
dt.Rows.Add(dr);
}
gvCorporateOverview.DataSource = dt;
gvCorporateOverview.DataBind();
}
}
}
{
using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oweb = osite.OpenWeb())
{
string currentYear = DateTime.Now.Year.ToString();
int year = Convert.ToInt32(currentYear);
string yearvalue = Convert.ToString(year - 1);
SPList olist = oweb.Lists.TryGetList("CorporateOverview");
SPQuery querry = new SPQuery();
string query = string.Format(@"<Where><Eq><FieldRef Name='FiscalYear'/><Value Type='Lookup'>{0}</Value></Eq></Where>", yearvalue);
querry.Query = query;
SPListItemCollection collListItems = olist.GetItems(querry);
DataTable dt = new DataTable();
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Description", typeof(string));
foreach (SPListItem item in collListItems)
{
DataRow dr = dt.NewRow();
dr["Title"] = item["Title"] != null ? item["Title"].ToString() : string.Empty;
dr["Description"] = item["Description"] != null ? item["Description"].ToString() : string.Empty;
dt.Rows.Add(dr);
}
gvCorporateOverview.DataSource = dt;
gvCorporateOverview.DataBind();
}
}
}
Comments