Solved

Display images from a folder in datagrid

Posted on 2008-10-29
11
1,178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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
 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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