Try the followings:
1. Add an
Image
field to the
GridView
and convert the field to
Template
field
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
2. Inside the
RowDataBound()
event method, find the
Image
control and assign the
ImageUrl
property as follows:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int Id = Convert.ToInt32(e.Row.DataItem);
Image img = e.Row.FindControl("Image1") as Image;
img.ImageUrl = GetImageUrl(Id);
}
}
Build an appropriate Image
Url
inside the
GetImageUrl(Id)
method using the Image Id. From the Database you know the actual physical location of the image and if the image is stored within the web application folder, you just need to calculate the relative path of the images based upon their physical path within the folder.
Hope this helps.