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

Retrieving Data from sharepoint to gridview without a single line of loop.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
public partial class Default3 : System.Web.UI.Page
{
DataTable table = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://vlnrm:100/"))
{
using (SPWeb web = site.OpenWeb())
{
SPQuery sQuery = new SPQuery();
sQuery.Query = "";
//SPList posts = web.Lists["Posts"];
DataTable table = web.Lists["Tasks"].GetItems(sQuery).GetDataTable();
GridView1.DataSource = table;
GridView1.DataBind();
}
}
});
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[4].Text=e.Row.Cells[4].Text.Substring(e.Row.Cells[4].Text.LastIndexOf(@"\")+1);
e.Row.Cells[5].Text = e.Row.Cells[5].Text.Substring(e.Row.Cells[5].Text.LastIndexOf(@".") + 1);
e.Row.Cells[5].Text = e.Row.Cells[5].Text.Substring(e.Row.Cells[5].Text.LastIndexOf(@"0") + 1);
}
}

No comments:

Post a Comment