TemplateColumn for this? conditionally display icon?

Discussion in 'ASP .Net Datagrid Control' started by djc, Nov 6, 2004.

  1. djc

    djc Guest

    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.
     
    djc, Nov 6, 2004
    #1
    1. Advertising

  2. djc wrote:
    > 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>
    </asp:TemplateColumn>

    > 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

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Nov 7, 2004
    #2
    1. Advertising

  3. djc

    djc Guest

    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!

    "Scott Mitchell [MVP]" <> wrote in message
    news:lRejd.18735$...
    > djc wrote:
    > > 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>
    > </asp:TemplateColumn>
    >
    > > 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
    >
    > http://www.4GuysFromRolla.com
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
     
    djc, Nov 8, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miro Durcik

    app.rc resource's icon to .net Icon

    Miro Durcik, Oct 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,404
    Miro Durcik
    Oct 9, 2004
  2. Danny Ni
    Replies:
    1
    Views:
    363
    Joe Fallon
    Aug 11, 2004
  3. Dan Polansky
    Replies:
    3
    Views:
    819
    Larry Barowski
    Feb 12, 2007
  4. djc

    conditionally display icon in template column?

    djc, Dec 13, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    168
  5. Bodi
    Replies:
    1
    Views:
    292
    Lasse Reichstein Nielsen
    Nov 18, 2003
Loading...

Share This Page