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

Working with SpGridview Hyperlink Fields Without a single line of loop


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Data;
using System.Globalization;
[assembly:AllowPartiallyTrustedCallers]
namespace TestSampleRD
{
    [Guid("d8484a38-f485-4eff-8532-7d5e5cf98a45")]
    public class TestSampleRD : System.Web.UI.WebControls.WebParts.WebPart
    {

        string strError = string.Empty;
        SPGridView sgvNews;
        public TestSampleRD()
        {
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(strError);
            try
            {
                writer.Write("<Table>");
                writer.Write("<Tr>");
                writer.Write("<Td Align='Center'>");
                writer.Write("<Strong>");
                writer.Write("News Headlines");
                writer.Write("</Strong>");
                writer.Write("</Td>");
                writer.Write("</Tr>");
                writer.Write("<Tr>");
                writer.Write("<Td>");
                writer.Write("<Center>");
                sgvNews.RenderControl(writer);
                writer.Write("</Center>");
                writer.Write("</Td>");
                writer.Write("</Tr>");
                writer.Write("</Table>");
            }
            catch (Exception ex)
            {
                writer.Write(ex.ToString());
            }
        }
        protected override void CreateChildControls()
        {
            try
            {
                SPWeb currentWeb = SPControl.GetContextWeb(Context);
                sgvNews = new SPGridView();
                sgvNews.ID = "sgvNews";
                sgvNews.AutoGenerateColumns = false;
                this.Controls.Add(sgvNews);

                HyperLinkField hTitle = new HyperLinkField();
                hTitle.HeaderText = "Title";
                hTitle.DataTextField = "Title";
                hTitle.DataNavigateUrlFields = new string[] { "ID" };
                hTitle.DataNavigateUrlFormatString = currentWeb.Url + "/Lists/World News/DispForm.aspx?ID={0}&Source=" + Page.Request.Url;
                sgvNews.Columns.Add(hTitle);
              

                BoundField bCategory = new BoundField();
                bCategory.HeaderText = "Category";
                bCategory.DataField = "Category";
                sgvNews.Columns.Add(bCategory);

                BoundField bArticleDate = new BoundField();
                bArticleDate.HeaderText = "Article Date";
                bArticleDate.DataField = "ArticleDate";
                bArticleDate.DataFormatString = "{0:dd/MM/yyyy}";
                sgvNews.Columns.Add(bArticleDate);

                BoundField bCreatedBy = new BoundField();
                bCreatedBy.HeaderText = "Created By";
                bCreatedBy.DataField = "Author";
                sgvNews.Columns.Add(bCreatedBy);

                HyperLinkField hEdit = new HyperLinkField();
                hEdit.HeaderText = "Edit";
                hEdit.Text = "Edit";
                hEdit.DataNavigateUrlFields = new string[] { "ID" };
                hEdit.DataNavigateUrlFormatString = currentWeb.Url + "/Lists/World News/EditForm.aspx?ID={0}&Source=" + Page.Request.Url;
                sgvNews.Columns.Add(hEdit);
               
                SPList NewsList = currentWeb.Lists["World News"];
                SPListItemCollection myColl = NewsList.Items;
                if (myColl.Count > 0)
                {
                    sgvNews.DataSource = myColl.GetDataTable();
                    sgvNews.DataBind();
                }
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
    }
}

No comments:

Post a Comment