Solved

Display images from a folder in datagrid

Posted on 2008-10-29
11
1,174 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Help with Query not working in client's PC 1 35
In WPF / C# binding a local database in code behind 1 24
SQL Login 17 40
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

21 Experts available now in Live!

Get 1:1 Help Now