?
Solved

Display images from a folder in datagrid

Posted on 2008-10-29
11
Medium Priority
?
1,182 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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 

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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

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