Bind List Items To a Grid Programatically
private void GetHRNewsletters()
{
using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oweb = osite.OpenWeb())
{
string currentYears = DateTime.Now.Year.ToString();
DataTable dtListItem = new DataTable();
dtListItem.Columns.Add("Title", typeof(string));
dtListItem.Columns.Add("URl", typeof(string));
dtListItem.Columns.Add("Month", typeof(string));
//dtListItem.Columns.Add("Month", typeof(string));
SPList olist = oweb.Lists.TryGetList("HRNewsletter");
SPQuery qurryclienttestmonialcollection = new SPQuery();
string stringQueryclienttestmonial = string.Format(@"<Where><Eq><FieldRef Name='FiscalYear' /><Value Type='Lookup'>{0}</Value></Eq></Where>", currentYears);
qurryclienttestmonialcollection.Query = stringQueryclienttestmonial;
SPListItemCollection col = olist.GetItems();
if (col.Count > 0)
{
foreach (SPListItem item in col)
{
DataRow row = dtListItem.NewRow();
row["URl"] = item["ows_FileRef"].ToString();
row["Title"] = item["ows_LinkFilename"].ToString();
row["Month"] = item["Month"].ToString();
dtListItem.Rows.Add(row);
}
}
grdDocument.DataSource = dtListItem;
grdDocument.DataBind();
}
}
}
{
using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oweb = osite.OpenWeb())
{
string currentYears = DateTime.Now.Year.ToString();
DataTable dtListItem = new DataTable();
dtListItem.Columns.Add("Title", typeof(string));
dtListItem.Columns.Add("URl", typeof(string));
dtListItem.Columns.Add("Month", typeof(string));
//dtListItem.Columns.Add("Month", typeof(string));
SPList olist = oweb.Lists.TryGetList("HRNewsletter");
SPQuery qurryclienttestmonialcollection = new SPQuery();
string stringQueryclienttestmonial = string.Format(@"<Where><Eq><FieldRef Name='FiscalYear' /><Value Type='Lookup'>{0}</Value></Eq></Where>", currentYears);
qurryclienttestmonialcollection.Query = stringQueryclienttestmonial;
SPListItemCollection col = olist.GetItems();
if (col.Count > 0)
{
foreach (SPListItem item in col)
{
DataRow row = dtListItem.NewRow();
row["URl"] = item["ows_FileRef"].ToString();
row["Title"] = item["ows_LinkFilename"].ToString();
row["Month"] = item["Month"].ToString();
dtListItem.Rows.Add(row);
}
}
grdDocument.DataSource = dtListItem;
grdDocument.DataBind();
}
}
}
Comments