ASP.Net

Displaying image in a gridview

0
(0)

Can not display images in gridview?

Many times I have seen people getting problem to show image in a gridview. This problem arises normally due to wrong image URL. This is because web server was not able to find the image.

Consider an example where, in my root directory I have a folder name images and my root directory also presents in a physical media(HardDrive). and I saved the image file in database with physical path (ex : c:\\mywebsite\images\a.jpg).

Then i tried to fetch the path from database and showing in gridview. At this time it will give the physical path to the image control. but in case of website, all data present in a virtual directory. So we need to give the virtual path to the image. Because when we open the page on browser it will search the content of images from virtual path.

When you save the path into database, just save the virtual path of the image. To get virtual path follow below code :

string filename=Server.MapPath(“~/images/a.jpg”);

Then you can save filename into database.

To show the image into gridview design gridview like below code :

<asp:GridView runat="server" ID="gv_Products" AutoGenerateColumns="false"  >

<Columns>
<asp:BoundField DataField="ProductNm" HeaderText="Product Name" ItemStyle-Height= "100px" ItemStyle-Width = "200px" />
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-Height= "100px" ItemStyle-Width = "200px"/>
<asp:TemplateField>
       <ItemTemplate>
               <img src='<%# Eval("ProductImgPath") %>' />
       </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

How useful was this post?

Click on a star to rate it!

Leave a Reply

Your email address will not be published. Required fields are marked *