showing images in datagrid

M

Mike P

I have a column in my datagrid where I want to show an image, which will
be different for each row. My sproc is returning a path to the image
for this column (e.g. /images/imagename.gif). How do I utilise this to
show the images in my datagrid?



Any help would be really appreciated!
 
G

Guest

Mike,
add a TemplateColumn with an image control:

<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:Image
Width="150" Height="125"
ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem,
"imageColumnName")) %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>



-- That's pseudocode, but you get the idea. You can do this in the
Properties page of the grid where you design columns.
Peter
 
M

Mike P

Peter,

Is FormatURL specific to .NET 2.0? I get an error whenever I use it and
I'm still using VS 2003. Here is my datagrid code and the sproc that
I'm getting the data from :

<asp:datagrid id="dgFixtures" CellPadding="3" Width="304" Runat="server"
BackColor="#FFFFFF" forecolor="#000000"
ShowHeader="True" AutoGenerateColumns="False"
BorderColor="black" HeaderStyle-Font-Bold="true"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-ForeColor="#00015E" Font-Size="8pt" Font-Name="arial"
BorderStyle="None" GridLines="None">
<Columns>
<asp:BoundColumn DataField="week" HeaderText="Week" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:Image runat="server" ImageUrl='<%#
FormatURL(DataBinder.Eval(Container.DataItem, "image")) %>'>
</asp:Image>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="opponent"
HeaderText="Opponent"></asp:BoundColumn>
<asp:BoundColumn DataField="result" HeaderText="Result"
/>
</Columns>
</asp:datagrid>

CREATE procedure nflc_fixture_list_Test
@season varchar(50) ,
@team varchar(50)
As
Select [Week] As Week,
Case When HomeTeam = @team
Then upper(AwayTeam)
Else HomeTeam
End As Opponent,
Case When HomeTeam = @team
Then Convert(VarChar(20), IsNull(HomeScore, '')) + '-' +
Convert(VarChar(20), IsNull(AwayScore, ''))
Else Convert(VarChar(20), IsNull(AwayScore, '')) + '-' +
Convert(VarChar(20), IsNull(HomeScore, ''))
End As Result,
'/images/small/' + replace(Case When HomeTeam = @team Then
AwayTeam Else HomeTeam End, ' ', '') + '.gif' As Image
From nflc_fixtures
Where Season = @season
And (AwayTeam = @team Or HomeTeam = @team)
Order By [Week]
GO

Any idea what I'm doing wrong?
 
G

Guest

FormatUrl just shows how to use a custom formatting function, such as
concatenation the image relative link with a complete uri. If you don't need
one, you can remove it.
Peter
 
P

Patrick.O.Ige

Adding to Peters advice
There are sample here at:-
http://aspalliance.com/141
Hope that helps
Patrick


Peter Bromberg said:
FormatUrl just shows how to use a custom formatting function, such as
concatenation the image relative link with a complete uri. If you don't need
one, you can remove it.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Mike P said:
Peter,

Is FormatURL specific to .NET 2.0? I get an error whenever I use it and
I'm still using VS 2003. Here is my datagrid code and the sproc that
I'm getting the data from :

<asp:datagrid id="dgFixtures" CellPadding="3" Width="304" Runat="server"
BackColor="#FFFFFF" forecolor="#000000"
ShowHeader="True" AutoGenerateColumns="False"
BorderColor="black" HeaderStyle-Font-Bold="true"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-ForeColor="#00015E" Font-Size="8pt" Font-Name="arial"
BorderStyle="None" GridLines="None">
<Columns>
<asp:BoundColumn DataField="week" HeaderText="Week" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:Image runat="server" ImageUrl='<%#
FormatURL(DataBinder.Eval(Container.DataItem, "image")) %>'>
</asp:Image>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="opponent"
HeaderText="Opponent"></asp:BoundColumn>
<asp:BoundColumn DataField="result" HeaderText="Result"
/>
</Columns>
</asp:datagrid>

CREATE procedure nflc_fixture_list_Test
@season varchar(50) ,
@team varchar(50)
As
Select [Week] As Week,
Case When HomeTeam = @team
Then upper(AwayTeam)
Else HomeTeam
End As Opponent,
Case When HomeTeam = @team
Then Convert(VarChar(20), IsNull(HomeScore, '')) + '-' +
Convert(VarChar(20), IsNull(AwayScore, ''))
Else Convert(VarChar(20), IsNull(AwayScore, '')) + '-' +
Convert(VarChar(20), IsNull(HomeScore, ''))
End As Result,
'/images/small/' + replace(Case When HomeTeam = @team Then
AwayTeam Else HomeTeam End, ' ', '') + '.gif' As Image
From nflc_fixtures
Where Season = @season
And (AwayTeam = @team Or HomeTeam = @team)
Order By [Week]
GO

Any idea what I'm doing wrong?
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top