Solved

Display images from a folder in datagrid

Posted on 2008-10-29
11
1,175 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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