How do you alternate images in a data bound column of a data grid control?

  • Thread starter Thread starter C Newby
  • Start date Start date
C

C Newby

I have a data grid control taht contains a data bound coulmn called "Type".
Depending on the value of "Type" I want to display a corresponding image in
the rendered table cell. So, if the value of type is 1, I want to show
image1.gif. If the value is 2, I want to show image2.gif. Is there an easy
way to do this?

TIA//
 
If SQL Server, you can do it in your statement:

, 'image'+imageType+'.gif' As ImageName

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Right, but i was hoping for something i could determine at run time. Any
thoughts on that? Thanks for your reply.
 
You can also write a function in your databinding expression for the image cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage is an accessible method (e.g. page.getmyimage), and parms is whatever you want to pass to get the image (the value of Type in this case)

----- C Newby wrote: ----

Right, but i was hoping for something i could determine at run time. An
thoughts on that? Thanks for your reply
 
Ah gotcha...thanks Bill, that's what i was looking for.

Bill Borg said:
You can also write a function in your databinding expression for the image
cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage is
an accessible method (e.g. page.getmyimage), and parms is whatever you want
to pass to get the image (the value of Type in this case).
 
Actually, that's not working for me. When the page renders, the table cell
just contains the expression. For example, i have something like:
<asp:BoundColumn DataField="Something" HeaderText="Something"
DataFormatString="<%=getSomething({0})%>"></asp:BoundColumn>

which renders:
<td><%=getSomething( SomeType )%></td>

But I do have a protected member method of the code behind class called
getSomething( someType Type ){ return "something"; }

Any thoughts?

thanks//


Bill Borg said:
You can also write a function in your databinding expression for the image
cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage is
an accessible method (e.g. page.getmyimage), and parms is whatever you want
to pass to get the image (the value of Type in this case).
 
You need to use a template column, not a bound column, which you can create in the property builder in the idea, or straight in the html. Bound column is good for showing something that resolves to text (and the format string can format it for you), but you need a picture in this case. Try something like this, where GetMyPicture is your function that returns the filename of the image you want to show. In my case, I'm passing container.dataitem("cs_key"), which will resolve to the cs_key field in the datasource. In your case, for cs_key you'll use the column name of your type field

<asp:TemplateColumn HeaderText="My Picture Column"><itemtemplate><img src='<%# GetMyPicture(container.dataitem("cs_key")) %>'/> (don't forget the '#'
</itemtemplate></asp:TemplateColumn

Regarding the protected function, that should be fine, but if at run-time it can't find it you'll get a semi-friendly message, something to the effect of "...GetMyPicture is not declared...

hth

Bil

----- C Newby wrote: ----

Actually, that's not working for me. When the page renders, the table cel
just contains the expression. For example, i have something like
<asp:BoundColumn DataField="Something" HeaderText="Something
DataFormatString="<%=getSomething({0})%>"></asp:BoundColumn

which renders
<td><%=getSomething( SomeType )%></td

But I do have a protected member method of the code behind class calle
getSomething( someType Type ){ return "something";

Any thoughts

thanks/


Bill Borg said:
You can also write a function in your databinding expression for the imag
cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage i
an accessible method (e.g. page.getmyimage), and parms is whatever you wan
to pass to get the image (the value of Type in this case)
 
Thanks again Bill,

I was able to get it working this time. One thing though,
"container.dataitem("myField")" didn't work for me, I ended up using:
"DataBinder.Eval(Container, "DataItem.myField")". Any thoughts on that?

Hey again, I really apreciate the help//

Bill Borg said:
You need to use a template column, not a bound column, which you can
create in the property builder in the idea, or straight in the html. Bound
column is good for showing something that resolves to text (and the format
string can format it for you), but you need a picture in this case. Try
something like this, where GetMyPicture is your function that returns the
filename of the image you want to show. In my case, I'm passing
container.dataitem("cs_key"), which will resolve to the cs_key field in the
datasource. In your case, for cs_key you'll use the column name of your
type field.
<asp:TemplateColumn HeaderText="My Picture Column"><itemtemplate><img
src=' said:
</itemtemplate></asp:TemplateColumn>


Regarding the protected function, that should be fine, but if at run-time
it can't find it you'll get a semi-friendly message, something to the effect
of "...GetMyPicture is not declared..."
hth,

Bill

----- C Newby wrote: -----

Actually, that's not working for me. When the page renders, the table cell
just contains the expression. For example, i have something like:
<asp:BoundColumn DataField="Something" HeaderText="Something"
DataFormatString="<%=getSomething({0})%>"></asp:BoundColumn>

which renders:
<td><%=getSomething( SomeType )%></td>

But I do have a protected member method of the code behind class called
getSomething( someType Type ){ return "something"; }

Any thoughts?

thanks//


the image
cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage is
an accessible method (e.g. page.getmyimage), and parms is whatever you want
to pass to get the image (the value of Type in this case). run time.
Any
 
Back
Top