TemplateColumn for this? conditionally display icon?

D

djc

I want to add a column to my datagrid that will display an icon depending on
what value is in a certian field in the database.

1) which type of column to use? TemplateColumn?
2) I'm looking for the best performing way to do this... my database field's
data will match the filename of the icon to display exactly. So, i'm hoping
there is a way I can kind of steam roll right through taking advantage of
that. Is there?

I guess a good question is this: Generally, how is a task like this
accomplished. I know there are usually 100 ways to do the same thing. Any
guidance is greatly appreciated. Thanks.
 
S

Scott Mitchell [MVP]

djc said:
I want to add a column to my datagrid that will display an icon depending on
what value is in a certian field in the database.

1) which type of column to use? TemplateColumn?
2) I'm looking for the best performing way to do this... my database field's
data will match the filename of the icon to display exactly. So, i'm hoping
there is a way I can kind of steam roll right through taking advantage of
that. Is there?

If you have the virtual path to the image file in your database field,
you can just use a TemplateColumn with an Image Web control in the
<ItemTemplate>, like so:

<asp:TemplateColumn ...>
<ItemTemplate>
<asp:Image runat="server" ... ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "databaseFieldThatPointsToImage") %>' />
</ItemTemplate>
I guess a good question is this: Generally, how is a task like this
accomplished. I know there are usually 100 ways to do the same thing. Any
guidance is greatly appreciated. Thanks.

In general, if you need to dynamically alter the markup generated based
on some column value you'll use a TemplateColumn. If you only need to
modify the *style* however (such as making the output bold if a database
value meets some criteria), then what I usually do is just use a
BoundColumn and muck with the style programmatically through the
ItemDataBound event.

If you are going to be doing a lot of work with the DataGrid, I'd like
to recommend my book: ASP.NET Data Web Controls Kick Start. It's 350+
pages focusing *just* on the DataGrid, DataList, and Repeater Web
controls. Learn more (and read a sample chapter) here:
http://www.4guysfromrolla.com/webtech/chapters/ASPDWC/

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
D

djc

Thank you Scott! I will buy your book. I had already decided to do that
after seeing the 4guysfromrolla website. I started reading your article on
the datagrid and I really like your writting style. (and that does mean
something because I read A LOT of tech books!) The answer you just provided
me is likely in that article but I have not finished it yet... ONLY reason
being fires needing attention on my 'day' job, which is actually network
administration rather than programming. I will be spending time on your site
and in your book I'm sure.

Thanks again!
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top