Home  »  Articles  »  ASP.Net  »

Displaying image in a gridview

 Posted By : Manoranjan SahooPosted Date : 19/07/2011 11:22:08 AM Category : ASP.Net Points : 3

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>

 

Responses
 Author : Manoj Sahoo Member Level : BronzeDate : 19/07/2011 11:35:23 AM Points : 2

nice article...

we can also use ImageField.

<asp:ImageField DataImageUrlField="ProductImgPath" HeaderText="Image" ></asp:ImageField>


 Author : Manoranjan Sahoo Member Level : BronzeDate : 19/07/2011 12:04:35 PM Points : 2

Hello Manoj,

Yes, We can use the ImageField also.

Thank you for your suggestion.



Post Reply
Login to post Response.