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

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//
 
C

Cowboy \(Gregory A. Beamer\)

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!
**********************************************************************
 
C

C Newby

Right, but i was hoping for something i could determine at run time. Any
thoughts on that? Thanks for your reply.
 
G

Guest

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
 
C

C Newby

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).
 
C

C Newby

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).
 
G

Guest

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)
 
C

C Newby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top