anesthetic
anesthetic

Reputation: 203

Gridview picture dependent on other column

I am making a gridview with 2 columns. Column 1 is a name value. Column 2 will have a picture that is dependent on the gender of the name in Column 1. Here is the code I have so far:

DataTable dy = new DataTable();
SqlDataAdapter dc = new SqlDataAdapter("SELECT name FROM recordTable", con);
dc.Fill(dy);
Grid1D.DataSource = dy;
Grid1D.DataBind();

and the asp part is as follows:

<asp:GridView ID="Grid1D" runat="server" CssClass="Grid1D"
    AutoGenerateColumns="false" Font-Names="Arial"
    Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B"
    HeaderStyle-BackColor="green" AllowPaging="true"
    PageSize="10">
    <Columns>
        <asp:ImageField DataImageUrlField="PictureURL"></asp:ImageField>
        <asp:TemplateField HeaderText="Name List">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("name") %>'
                    ID="lblname" SelectedRowStyle="myselection" CssClass="reclabel" Width="100%" AutoPostBack="true" onclick="newrec()"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

I'm just not sure how to populate the imagefield with images A and B dependent on gender. I know how to do it if there were a set number of rows but it's not consistent.

Upvotes: 0

Views: 57

Answers (1)

VDWWD
VDWWD

Reputation: 35564

You could do this. But as rene pointed out you would need more columns, like

Name, Gender, image1 and image2

Then do this

<asp:TemplateField>
    <ItemTemplate>

        <%# Eval("gender").ToString() == "F" ? "<img src=\"female.jpg\">" : "<img src=\"male.jpg\">"  %>    
        or    
        <%# Eval("gender").ToString() == "F" ? "<img src=\"" + Eval("image1").ToString() + "\">" : "<img src=\"" + Eval("image2").ToString() + "\">"  %>

    </ItemTemplate>
</asp:TemplateField>

Upvotes: 1

Related Questions