Solved

How to display images in SPGridView column

Posted on 2011-03-07
3
1,154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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