Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Display An Image In A Datagrid Dynamically

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Iain - 06 Sep 2007 09:15 GMT
Hi All

Hope someone can assist.
Using Borland Developer Studio 2006, C# .Net Application.
I have a datagrid the source of whioch is a database table.
I can populate the datagrid with no problems.
The first column in the datagrid contains the url for an image which I
want to display in column 6 of the same row.
The image is stored in a sub-directory.

Can anyone please advise me how I would accomplish this.
I have included the relevant snippets of code below.

Thanks in advance for any assistance offered

Iain

------------------------------------------------------------------------------------------------------------------------------------------------------------

<asp:datagrid id="PageImages" runat="server" width="82%"
borderwidth="1px" borderstyle="Solid"
                                              bordercolor="#C0FFFF"
                          enableviewstate="true" allowsorting="True">
 <Columns>
   <ASP:BoundColumn datafield="ImageName" headertext="Image Name"></
ASP:BoundColumn>
   <ASP:BoundColumn datafield="ImageText" headertext="Image text"></
ASP:BoundColumn>
   <ASP:BoundColumn datafield="AlternateText" headertext="Alternate
text"></ASP:BoundColumn>
   <ASP:BoundColumn datafield="HRef" headertext="Internet Address
(URL)"></ASP:BoundColumn>
   <ASP:BoundColumn datafield="Sequence" headertext="Display
Sequence"></ASP:BoundColumn>
   <ASP:BoundColumn datafield="Published" headertext="Published"></
ASP:BoundColumn>
   <asp:TemplateColumn HeaderText="Displayed Image">
     <ItemTemplate>
       <asp:Image Runat="server" ID="Image1" />
     </ItemTemplate>
   </asp:TemplateColumn>
   </Columns>
</asp:datagrid>

---------------------------------------------------------------
Code Behind
---------------------------------------------------------------

public void PageImages_OnItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
 {
 if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
   {
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
     {
     // get the image name from the first column : No Problem Here
     string LImageName = e.Item.Cells[0].Text;
     // *************************************************************
     // At this point I want to add an image to the cell
     // using the image name taken from the first column
     e.Item.Cells[6]................................. = LImageName;
     // *************************************************************
     }
   }
 }
marss - 11 Sep 2007 12:59 GMT
> Hi All
>
[quoted text clipped - 63 lines]
>     }
>   }

> public void PageImages_OnItemDataBound(object sender,
> System.Web.UI.WebControls.DataGridItemEventArgs e)
[quoted text clipped - 14 lines]
>       }
>     }

Image Image1 = e.Item.FindControl("Image1") as Image;
Image1.ImageUrl = LImageName;

Also these included checks are not required, If ItemType is
ListItemType.Item, then it is definitely not a ListItemType.Header or
ListItemType.Footer.

Check  (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) is enough.

Regards,
Mykola
http://marss.co.ua

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.