Skip to content

Conditional Format image

Andy Woodward

Asked Jan 5, 2010, 9:25 AM EST1 answer
Share
LinkedInX
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

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    hi,
    You can do this in item databound event of gridview , check the value of column and depending upon it u can set value of other column.
    0