Solved

How to display images in SPGridView column

Posted on 2011-03-07
3
1,141 Views
Last Modified: 2012-06-27
How do I display an image spgridview? I believe I need to create a custom field and bind it to the SPGridView.  I also need the image to generate a tool tip on a hover over.
0
Comment
Question by:jasonshifflett
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Minesh Shah
ID: 35373656
0
 

Accepted Solution

by:
jasonshifflett earned 0 total points
ID: 36328535
I dont know if there is an easier way but I was able to create a custom field by creating an object that uses the ITemplate interface
using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


namespace BRTRC.App.WhitePages.Assembly.WebControls
{
    class TemplateImageDisplay :ITemplate
    {
        ListItemType _TemplateType;
        private string _ColumnName;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="colname"></param>
        public TemplateImageDisplay(ListItemType type, string colname)
          {
              _TemplateType = type;
              _ColumnName = colname;
          }
         /// <summary>
         /// 
         /// </summary>
         /// <param name="container"></param>
          public void InstantiateIn( Control container )
          {
              Literal lc = new Literal();

              switch (_TemplateType)
              {

                  case ListItemType.Header:
                      lc.Text = _ColumnName;
                      container.Controls.Add(lc);
                      break;
                  case ListItemType.Item:
                      Literal l = new Literal();
                      l.DataBinding += new EventHandler(button_DataBinding);
                      container.Controls.Add(l);
                      break;
                  default:
                      break;

              }
          }
         /// <summary>
         /// 
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
          void button_DataBinding( object sender, EventArgs e )
          {
              Literal l = (Literal)sender;
              string localText = String.Empty;
              SPGridViewRow container = (SPGridViewRow)l.NamingContainer;
              bool imageFound = false;
              localText = ((DataRowView)container.DataItem)[_ColumnName].ToString();

              try
              {

                  if (localText.ToLower().Contains(".jpg,"))
                  {
                      localText = localText.Substring(0, localText.ToLower().IndexOf(".jpg,") + 4);
                      imageFound = true;
                  }
                  if (localText.ToLower().Contains(".png,") && !imageFound) 
                  {
                      localText = localText.Substring(0, localText.ToLower().IndexOf(".png,") + 4);
                      imageFound = true;
                  
                  }

                  if (!imageFound)
                  {
                      l.Text = Constants.UNKNOWNIMAGE;
                  }
                  else {

                      l.Text = "<img class=\"ImageSize\"src=\"" + localText + "\"/>";
                  }
                  
                  
              }
              catch { l.Text = Constants.UNKNOWNIMAGE; }//Unknown-Person.jpg
              
          
          
          }

    }
}

Open in new window

0
 

Author Closing Comment

by:jasonshifflett
ID: 36361644
It worked and it lays out a blue print to create future custom fields
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently came across an issue with a MOSS 2007 deployment where access into some sub-sites were denied, even for the MOSS farm administrators. A bit of background to the setup of this MOSS farm; this was a three server setup, consisting of a fr…
I used to be SharePoint evangelist in our company, so my Outlook always full of questions about how to do this, or where I can find that. One day I found such an email with the following question: "how to attach 3-State workflow (one of the workflow…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question