This is a migrated thread and some comments may be shown as answers.

Display dynamic Image for Boolean column data instead of using GridCheckBoxColumn

5 Answers 280 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rich
Top achievements
Rank 2
Rich asked on 27 Apr 2009, 07:38 PM
Hi -

I would like to use an image as a boolean indicator (e.g. "off.gif" and "on.gif") instead of using something the GridCheckBoxColumn.  Is there a solution that somebody has already done for this?  I couldn't find one searching this forum and I would think its not a unique usage of a boolean flag.

I know one solution can be a template column with an <asp:Image> and we can swap out the image source in the proper code event, but we have a lot of places to use this pattern in multiple grids/pages and would like to figure out some way to encapsulate it.

Thanks!
Rich

5 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 28 Apr 2009, 04:24 AM
Hello Rich,

One suggestion would be using GridTemplateColumn with Image control and assigning the image in ASPX itself as shown below.

ASPX:
 
<telerik:GridTemplateColumn UniqueName="TemplateColumn" Groupable="False"
      <ItemTemplate> 
           <asp:Image ID="ImportantImage" BorderWidth="0px" ImageUrl='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Status"))? "img/on.gif" : "img/off.gif" %>' 
                AlternateText="Status" Style="float: right; cursor: pointer;" runat="server"
           </asp:Image> 
      </ItemTemplate> 
</telerik:GridTemplateColumn> 

Thanks,
Shinu.
0
Rich
Top achievements
Rank 2
answered on 28 Apr 2009, 10:55 AM
Hi Shinu -

Thanks! That's exactly what we decided on last night but you did it one better with the naming of the "on.gif"/"off.gif" in the declarative markup.  We were going to have those as additional columns returned in the data using sql case when's, but didn't exactly like embedding that information at that layer.  Being able to keep it at the markup level is perfect!

You are a Master!

Rich
0
Grey
Top achievements
Rank 1
answered on 25 Feb 2010, 11:50 AM
Do you know how one could do the same thing in VB ?
0
Rich
Top achievements
Rank 2
answered on 25 Feb 2010, 03:37 PM
Hi Grey -

I don't use VB.NET so I may not be fully understanding your question, but Shinu's post should work for either C# or VB.NET as it is just declarative markup.

Rich
0
Grey
Top achievements
Rank 1
answered on 25 Feb 2010, 09:40 PM
Odd that

'cos when I use that cut 'n pasted code I get

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC36637: The '?' character cannot be used here.


      <ItemTemplate>  
           <asp:Image ID="ImportantImage" BorderWidth="0px" ImageUrl='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "assocmember"))? "img/on.gif" : "img/off.gif" %>'  
                AlternateText="Status" Style="float: right; cursor: pointer;" runat="server">  
           </asp:Image>  
      </ItemTemplate>  

Tags
Grid
Asked by
Rich
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Rich
Top achievements
Rank 2
Grey
Top achievements
Rank 1
Share this question
or