Display An Image In A Datagrid Dynamically

I

Iain

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;
// *************************************************************
}
}
}
 
M

marss

Hi All

Hope someone can assist.
Using Borland Developer Studio 2006, C# .Net Application.
I have adatagridthe source of whioch is a database table.
I can populate thedatagridwith no problems.
The first column in thedatagridcontains 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:datagridid="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;
// *************************************************************
}
}
}
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;
// *************************************************************
}
}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top