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, February 27, 2012

Showing the SpGridView Version 2010 same like SpGridView Version 2007

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;

namespace PrasadVWebPart.PrasadVWebPart
{
    [ToolboxItemAttribute(false)]
    public class PrasadVWebPart : WebPart
    {
        string strError = string.Empty;
        SPGridView sgvTasks;
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(strError);
            try
            {
                writer.Write("<Style type='Text/Css'>");
                writer.Write(".GridClass");
                writer.Write("{");
                writer.Write("text-Align:Left;font-Weight:bold;");
                writer.Write("}");
                writer.Write("</Style>");
                sgvTasks.RenderControl(writer);
            }
            catch (Exception ex)
            {

                writer.Write(ex.ToString());
            }
        }
        protected override void CreateChildControls()
        {
            try
            {
                sgvTasks = new SPGridView();
                sgvTasks.ID = "sgvTasks";
                sgvTasks.AutoGenerateColumns = false;
                this.Controls.Add(sgvTasks);

                SPWeb currentWeb = SPContext.Current.Web;
                HyperLinkField hTaskName = new HyperLinkField();
                hTaskName.HeaderStyle.CssClass = "GridClass";
                hTaskName.HeaderText = "Task Name";
                hTaskName.DataNavigateUrlFields = new String[] { "ID" };
                hTaskName.DataTextField = "Title";
                hTaskName.DataNavigateUrlFormatString = currentWeb.Url + "/Lists/Tasks/DispForm.aspx?ID={0}&Source=" + Page.Request.Url;
                sgvTasks.Columns.Add(hTaskName);

                BoundField bStatus = new BoundField();
                bStatus.HeaderStyle.CssClass = "GridClass";
                bStatus.HeaderText = "Status";
                bStatus.DataField = "Status";
                sgvTasks.Columns.Add(bStatus);

                BoundField bPriority = new BoundField();
                bPriority.HeaderStyle.CssClass = "GridClass";
                bPriority.HeaderText = "Priority";
                bPriority.DataField = "Priority";
                sgvTasks.Columns.Add(bPriority);

                BoundField bCreatedDate = new BoundField();
                bCreatedDate.HeaderStyle.CssClass = "GridClass";
                bCreatedDate.HeaderText = "Created Date";
                bCreatedDate.DataField = "Created";
                bCreatedDate.DataFormatString = "{0:dd-MMM-yyyy}";
                sgvTasks.Columns.Add(bCreatedDate);
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPList lstTasks = currentWeb.Lists["Tasks"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                    sQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Priority' /><FieldRef Name='Status' /><FieldRef Name='Created' /><FieldRef Name='ID' />";
                    sQuery.ViewFieldsOnly = true;
                    SPListItemCollection TasksColl = lstTasks.GetItems(sQuery);
                    if (TasksColl.Count > 0)
                    {
                        sgvTasks.DataSource = TasksColl.GetDataTable();
                        sgvTasks.DataBind();
                    }
                });
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
    }
}

No comments:

Post a Comment