Solved

How to display images in SPGridView column

Posted on 2011-03-07
3
1,114 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I have just recently built a new SharePoint 2007 farm on a complete Windows 2008 R2 server platform and part of my standard build procedure is to implement a warm up routine, usually in the form of a script that is scheduled every morning to launch …
For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could add the jQuery l…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now