Tuesday, January 3, 2012

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://agmsm:9999/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                    //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