Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to display images in SPGridView column

Posted on 2011-03-07
3
Medium Priority
?
1,174 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
 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

824 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