all groups > asp.net > january 2006 >
You're in the

asp.net

group:

showing images in datagrid


showing images in datagrid Mike P
1/21/2006 7:15:11 PM
asp.net:
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!


RE: showing images in datagrid Peter Bromberg [C# MVP]
1/21/2006 7:37:02 PM
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


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




[quoted text, click to view]
RE: showing images in datagrid Mike P
1/21/2006 8:32:10 PM
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?




RE: showing images in datagrid Peter Bromberg [C# MVP]
1/22/2006 5:20:02 AM
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




[quoted text, click to view]
Re: showing images in datagrid Patrick.O.Ige
1/23/2006 11:36:40 PM
Adding to Peters advice
There are sample here at:-
http://aspalliance.com/141
Hope that helps
Patrick


[quoted text, click to view]

AddThis Social Bookmark Button