1
Answer

Conditional Format image

Andy Woodward

Andy Woodward

14y
3.7k
1
Hi.

I have a gridview which binds to a sql database.
I need to add an image to the gridview on condition of the column "Difference".
If the value of "Difference" is less than 2.5 then Good.jpg is displayed in a column called Rating. If the value of "Difference" is equal to 2.5 then Average.jpg should be displayed and if the value of "Difference" is greater than 2.5 then Bad.jpg should be displayed.
The data type of "Difference" is float.
So far I have converted the column "Rating" to a template and added <asp:Image> to the <ItemTemplate>.
Code is:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                        DataSourceID="scoreFinLastMonthSrc">
                        <Columns>
                            <asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
                            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                            <asp:BoundField DataField="Target" HeaderText="Target" ReadOnly="True"
                                SortExpression="Target" />
                            <asp:BoundField DataField="Actual" HeaderText="Actual" ReadOnly="True"
                                SortExpression="Actual"/>
                            <asp:TemplateField HeaderText="Rating" SortExpression="Rating">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Rating") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# GetImage(Convert.Float(DataBinder.Eval(Container.DataItem, "Difference"))) %>'></asp:Image>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <asp:SqlDataSource ID="scoreFinLastMonthSrc" runat="server"
                        ConnectionString="<%$ ConnectionStrings:SSDashboardConnectionString %>"
                        SelectCommand="SELECT Code, Title, '£' + CONVERT (varchar(12), CONVERT (money, Target), 1) AS Target, '£' + CONVERT (varchar(12), CONVERT (money, Actual), 1) AS Actual, Rating, Difference FROM ScoreFinance">
                    </asp:SqlDataSource>
I need to add a function in the code behind file to do the conditional format called GetImage.
I am using C# in the code behind.

Any help is gratefully received.

Andy
Answers (1)
Next Recommended Forum