Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Binding Data to SpGridView using SharePoint Server Side Code and Dotnet Linq

Null checking with Linq Server Side Code
using System;

using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace SharePointProject1.SharePointProject1
{
[ToolboxItemAttribute(false)]
public class SharePointProject1 : WebPart
{
protected override void Render(HtmlTextWriter writer)
{
SPList lstSC = SPContext.Current.Web.Lists["SharePoint 2010 Concepts"];
SPListItemCollection myColl = lstSC.Items;
string strTitle = string.Empty;
string strStatus = string.Empty;
if (lstSC.Items.Count> 0)
{
var query = from SPListItem item in lstSC.Items
where item["Status"].ToString().Substring(item["Status"].ToString().LastIndexOf("#") + 1)!="Completed"
orderby item["ID"] descending
select new
{
strTitle=item.Title,
strStatus=item["Status"].ToString().Substring(item["Status"].ToString().LastIndexOf("#") + 1)
};
SPGridView sgvConcepts = new SPGridView();
sgvConcepts.Attributes.Add("RunAt", "Server");
sgvConcepts.AutoGenerateColumns = false;
BoundField bTitle = new BoundField();
bTitle.HeaderText = "Title";
bTitle.DataField = "strTitle";
sgvConcepts.Columns.Add(bTitle);
BoundField bStatus = new BoundField();
bStatus.HeaderText = "Status";
bStatus.DataField = "strStatus";
sgvConcepts.Columns.Add(bStatus);
this.Controls.Add(sgvConcepts);
sgvConcepts.DataSource = query;
sgvConcepts.DataBind();
sgvConcepts.RenderControl(writer);
}
}
protected override void CreateChildControls()
{
}
}
}

1 comment: