thegunner
thegunner

Reputation: 7163

Display image in gridview depending on value of column

I have a gridview which displays rows and columns all linked to an sql statement... and all works as normal.

I want to include a new column, that displays an image depending on what the value of the column is. If the column has a value - it will display an image. If the column value is null no image whill be displayed.

My sql is something like:

SELECT c.call_id, title, a.call_id as b_attach
FROM calls c 
LEFT JOIN attachments a ON c.call_id = a.call_id 
GROUP BY c.call_id,title,description, a.call_id

What's returns from this sql is:

Call_id | title | b_attach
1235 | title goes here | 1235
1382 | another title |NULL

So if there's something in b_attach - diplay image in gridview column, else display nothing in gridview column

My Gridview:

                        <asp:HyperLinkField SortExpression="call_id" HeaderText="Call id" DataTextField="call_id"
                            DataNavigateUrlFields="call_id" DataNavigateUrlFormatString="showcall.aspx?id={0}" />
                        <asp:HyperLinkField SortExpression="Title" HeaderText="Title" DataTextField="title"
                            DataNavigateUrlFields="call_id" DataNavigateUrlFormatString="showcall.aspx?id={0}" />

                    </Columns>

Any ideas on how to do this?

Upvotes: 1

Views: 2479

Answers (1)

onof
onof

Reputation: 17377

You can use a TemplateField, and, inside it something like this:

<asp:Image runat="server" id="myImg" ImageUrl='<%# GetImage(DataBinder.Eval(Container.DataItem, "b_attach")) >%' visible='<%# null != DataBinder.Eval(Container.DataItem, "b_attach") %> />

Upvotes: 1

Related Questions