This Gridview sample shows how to, for each row, based on other data within that row, to show a different image. We do this by creating a TemplateField, and putting an ASP.Net Image control within it, called ‘Image1′.
Then, inside the RowDataBound event of the Gridview, we put code, which first, checks and finds the Image control in that row, and then assigning a different JPG file to the ImageURL property of that image. One other thing here, you’ll notice, is that when the criteria is matched, we set the Image control’s Visible property to ‘True’. The defalut Image control’s Visible property to ‘False’.
Naturally, since we’re checking one particular column in the Gridview for this data, we’re using a Select Case statement.
For this we create class to load the data in gridview. You can load data in gridview from database and use this technic to show conditional image.

Here is the html markup
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_02_Gridview_Conditional_Images._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HtmlEncode="False"
HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="Discount" HeaderText="Discount" SortExpression="Discount" HtmlEncode="False" Visible="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-asp.net.png" alt= "Smart Social Share" href="http://codecanyon.net/item/smart-social-share/160097" title="Smart Social Share (Asp.net control)" description="Smart Social share is a asp.net control . Which helps you to give user to share your content with social book mark site." ref="marifdu"]
The class to load Product .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Product
{
public Product()
{
}
public Product(int productid, string productname,
int unitinstock, int unitprice,int discount
)
{ this.ProductID = productid;
this.ProductName = productname;
this.UnitsInStock = unitinstock;
this.UnitPrice = unitprice;
this.Discount = discount;
}
private int _productid;
public int ProductID
{
get { return _productid; }
set { _productid = value; }
}
private string _productname;
public string ProductName
{
get { return _productname; }
set { _productname = value; }
}
private int _unitinstock;
public int UnitsInStock
{
get { return _unitinstock; }
set { _unitinstock = value; }
}
private int _unitprice;
public int UnitPrice
{
get { return _unitprice; }
set { _unitprice = value; }
}
private int _discount;
public int Discount
{
get { return _discount; }
set { _discount = value; }
}
public static List<Product> GetList()
{
List<Product> product = new List<Product>();
product.Add(new Product(1, "Chai", 39, 18,15));
product.Add(new Product(2, "Chef Anton's Cajun Seasoning", 3, 12,25));
product.Add(new Product(3, "Grandma's Boysenberry Spread" ,120 ,25,30));
product.Add(new Product(4, "Uncle Bob's Organic Dried Pears", 15 ,30,50));
return product;
}
}
Finally the code behind the defalult.aspx page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _02_Gridview_Conditional_Images
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
public void DataBind()
{
GridView1.DataSource = Product.GetList();
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = (Image)e.Row.FindControl("image1");
switch (int.Parse(e.Row.Cells[4].Text))
{
case 15:
img.ImageUrl = "/images/discount_15.jpg";
img.Visible = true;
break;
case 25:
img.ImageUrl = "/images/discount_25.jpg";
img.Visible = true;
break;
case 30:
img.ImageUrl = "/images/discount_30.jpg";
img.Visible = true;
break;
case 50:
img.ImageUrl = "/images/discount_50.jpg";
img.Visible = true;
break;
default:
img.Visible = false;
break;
}
}
}
}
}
You can download the code from bellow link.