Solved

Display images from a folder in datagrid

Posted on 2008-10-29
11
1,173 Views
Last Modified: 2013-11-26
Hi
I try to display images from a folder called media in datagrid.
The code in code snippet displays images which are placed in the root of the project under solution explorer. I wonder how I can make it to display images from the folder media instead.
Thanks
<asp:ImageField DataImageUrlField="imagename" HeaderText="Foto" />

Open in new window

0
Comment
Question by:sam20
  • 6
  • 5
11 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830022
I think you'll need to do this on the ItemDataBound.

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
System.Web.UI.WebControls.Image anImagine ;
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
{
anImagine = ((System.Web.UI.WebControls.Image)e.Item.Cells[1].Controls[1]); anImagine.ImageUrl="icons/" + e.Item.Cells[0].Text.ToString() + ".gif";
}
}
0
 

Author Comment

by:sam20
ID: 22830131
Hi Dhaest
Thanks for your answer.
Should I go to edit columns and open the fields dialog or just add the code you suggested.?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830194
Are you using a databound datagrid ? How do you know which image to show in which row ? Does teh name come from a column in the database ?
0
 

Author Comment

by:sam20
ID: 22830251
yes I am using a databound data grid. right image shows in right rows, but it displays just images which are placed in the root of the project under solution explorer no from the folder called media.
This is address what shows in propoties windows: http://localhost:1891/proevo.jpg
Righr address should be:http://localhost:1891/media/proevo.jpg
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830292
Add the code I gave you above in your project. Add a breakpoint and try to see if you are getting the right imagename in your debug-session.

It will be in e.Item.Cells[0].Text.ToString()
(but the 0 can be 1,2, ... depending in which column you are setting your image)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:sam20
ID: 22830328
Where in my object should I add the code?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830343
This needs to be in your codebehind file.
0
 

Author Comment

by:sam20
ID: 22830482
I have added the code into default.aspx.cs under:
 public partial class _Default : System.Web.UI.Page
{
But got no any effect.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22830515
How is your datagrid called ?

You can also click on your datagrid, and on the properties-screen, you'll see all the possible events.
Doubleclick on the ItemDataBound-event and it will be generate a base-event
0
 

Author Comment

by:sam20
ID: 22830633

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="id" DataSourceID="ContentListDataSource" ForeColor="#333333" GridLines="None" Width="732px">

Open in new window

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 22830675
It's a gridview, not a datagrid :) -> Then I don't think you have a itemDataBound (I'm not sure, will have to see for it, but anyway, the name should be the same as your gridname)

private void gridview_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
System.Web.UI.WebControls.Image anImagine ;
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
{
anImagine = ((System.Web.UI.WebControls.Image)e.Item.Cells[1].Controls[1]); anImagine.ImageUrl="icons/" + e.Item.Cells[0].Text.ToString() + ".gif";
}
}

GridView Examples for ASP.NET 2.0: Displaying Images in a GridView Column
http://msdn.microsoft.com/en-us/library/aa479350.aspx

Loading Images Asynchronously Inside an ASP.NET GridView
http://www.dotnetcurry.com/ShowArticle.aspx?ID=193&AspxAutoDetectCookieSupport=1
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now