Solved

How to display images in SPGridView column

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

21 Experts available now in Live!

Get 1:1 Help Now