How do you alternate images in a data bound column of a data grid control?

Discussion in 'ASP .Net' started by C Newby, Apr 1, 2004.

  1. C Newby

    C Newby Guest

    I have a data grid control taht contains a data bound coulmn called "Type".
    Depending on the value of "Type" I want to display a corresponding image in
    the rendered table cell. So, if the value of type is 1, I want to show
    image1.gif. If the value is 2, I want to show image2.gif. Is there an easy
    way to do this?

    TIA//
    C Newby, Apr 1, 2004
    #1
    1. Advertising

  2. If SQL Server, you can do it in your statement:

    , 'image'+imageType+'.gif' As ImageName

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    **********************************************************************
    Think Outside the Box!
    **********************************************************************
    "C Newby" <> wrote in message
    news:...
    > I have a data grid control taht contains a data bound coulmn called

    "Type".
    > Depending on the value of "Type" I want to display a corresponding image

    in
    > the rendered table cell. So, if the value of type is 1, I want to show
    > image1.gif. If the value is 2, I want to show image2.gif. Is there an easy
    > way to do this?
    >
    > TIA//
    >
    >
    Cowboy \(Gregory A. Beamer\), Apr 1, 2004
    #2
    1. Advertising

  3. C Newby

    C Newby Guest

    Right, but i was hoping for something i could determine at run time. Any
    thoughts on that? Thanks for your reply.



    "Cowboy (Gregory A. Beamer)" <> wrote in
    message news:...
    > If SQL Server, you can do it in your statement:
    >
    > , 'image'+imageType+'.gif' As ImageName
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > **********************************************************************
    > Think Outside the Box!
    > **********************************************************************
    > "C Newby" <> wrote in message
    > news:...
    > > I have a data grid control taht contains a data bound coulmn called

    > "Type".
    > > Depending on the value of "Type" I want to display a corresponding image

    > in
    > > the rendered table cell. So, if the value of type is 1, I want to show
    > > image1.gif. If the value is 2, I want to show image2.gif. Is there an

    easy
    > > way to do this?
    > >
    > > TIA//
    > >
    > >

    >
    >
    C Newby, Apr 1, 2004
    #3
  4. You can also write a function in your databinding expression for the image cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage is an accessible method (e.g. page.getmyimage), and parms is whatever you want to pass to get the image (the value of Type in this case)

    ----- C Newby wrote: ----

    Right, but i was hoping for something i could determine at run time. An
    thoughts on that? Thanks for your reply



    "Cowboy (Gregory A. Beamer)" <> wrote i
    message news:..
    > If SQL Server, you can do it in your statement
    >> , 'image'+imageType+'.gif' As ImageNam
    >> --

    > Gregory A. Beame
    > MVP; MCP: +I, SE, SD, DB
    >> *********************************************************************

    > Think Outside the Box
    > *********************************************************************
    > "C Newby" <> wrote in messag
    > news:..
    >> I have a data grid control taht contains a data bound coulmn calle

    > "Type"
    >> Depending on the value of "Type" I want to display a corresponding imag

    > i
    >> the rendered table cell. So, if the value of type is 1, I want to sho
    >> image1.gif. If the value is 2, I want to show image2.gif. Is there a

    eas
    >> way to do this
    >>>> TIA/
    >>>>>>
    =?Utf-8?B?QmlsbCBCb3Jn?=, Apr 1, 2004
    #4
  5. C Newby

    C Newby Guest

    Ah gotcha...thanks Bill, that's what i was looking for.

    "Bill Borg" <> wrote in message
    news:...
    > You can also write a function in your databinding expression for the image

    cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage is
    an accessible method (e.g. page.getmyimage), and parms is whatever you want
    to pass to get the image (the value of Type in this case).
    >
    > ----- C Newby wrote: -----
    >
    > Right, but i was hoping for something i could determine at run time.

    Any
    > thoughts on that? Thanks for your reply.
    >
    >
    >
    > "Cowboy (Gregory A. Beamer)" <>

    wrote in
    > message news:...
    > > If SQL Server, you can do it in your statement:
    > >> , 'image'+imageType+'.gif' As ImageName
    > >> --

    > > Gregory A. Beamer
    > > MVP; MCP: +I, SE, SD, DBA
    > >>

    **********************************************************************
    > > Think Outside the Box!
    > >

    **********************************************************************
    > > "C Newby" <> wrote in message
    > > news:...
    > >> I have a data grid control taht contains a data bound coulmn

    called
    > > "Type".
    > >> Depending on the value of "Type" I want to display a corresponding

    image
    > > in
    > >> the rendered table cell. So, if the value of type is 1, I want to

    show
    > >> image1.gif. If the value is 2, I want to show image2.gif. Is there

    an
    > easy
    > >> way to do this?
    > >>>> TIA//
    > >>>>>>
    C Newby, Apr 1, 2004
    #5
  6. C Newby

    C Newby Guest

    Actually, that's not working for me. When the page renders, the table cell
    just contains the expression. For example, i have something like:
    <asp:BoundColumn DataField="Something" HeaderText="Something"
    DataFormatString="<%=getSomething({0})%>"></asp:BoundColumn>

    which renders:
    <td><%=getSomething( SomeType )%></td>

    But I do have a protected member method of the code behind class called
    getSomething( someType Type ){ return "something"; }

    Any thoughts?

    thanks//


    "Bill Borg" <> wrote in message
    news:...
    > You can also write a function in your databinding expression for the image

    cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage is
    an accessible method (e.g. page.getmyimage), and parms is whatever you want
    to pass to get the image (the value of Type in this case).
    >
    > ----- C Newby wrote: -----
    >
    > Right, but i was hoping for something i could determine at run time.

    Any
    > thoughts on that? Thanks for your reply.
    >
    >
    >
    > "Cowboy (Gregory A. Beamer)" <>

    wrote in
    > message news:...
    > > If SQL Server, you can do it in your statement:
    > >> , 'image'+imageType+'.gif' As ImageName
    > >> --

    > > Gregory A. Beamer
    > > MVP; MCP: +I, SE, SD, DBA
    > >>

    **********************************************************************
    > > Think Outside the Box!
    > >

    **********************************************************************
    > > "C Newby" <> wrote in message
    > > news:...
    > >> I have a data grid control taht contains a data bound coulmn

    called
    > > "Type".
    > >> Depending on the value of "Type" I want to display a corresponding

    image
    > > in
    > >> the rendered table cell. So, if the value of type is 1, I want to

    show
    > >> image1.gif. If the value is 2, I want to show image2.gif. Is there

    an
    > easy
    > >> way to do this?
    > >>>> TIA//
    > >>>>>>
    C Newby, Apr 1, 2004
    #6
  7. You need to use a template column, not a bound column, which you can create in the property builder in the idea, or straight in the html. Bound column is good for showing something that resolves to text (and the format string can format it for you), but you need a picture in this case. Try something like this, where GetMyPicture is your function that returns the filename of the image you want to show. In my case, I'm passing container.dataitem("cs_key"), which will resolve to the cs_key field in the datasource. In your case, for cs_key you'll use the column name of your type field

    <asp:TemplateColumn HeaderText="My Picture Column"><itemtemplate><img src='<%# GetMyPicture(container.dataitem("cs_key")) %>'/> (don't forget the '#'
    </itemtemplate></asp:TemplateColumn

    Regarding the protected function, that should be fine, but if at run-time it can't find it you'll get a semi-friendly message, something to the effect of "...GetMyPicture is not declared...

    hth

    Bil

    ----- C Newby wrote: ----

    Actually, that's not working for me. When the page renders, the table cel
    just contains the expression. For example, i have something like
    <asp:BoundColumn DataField="Something" HeaderText="Something
    DataFormatString="<%=getSomething({0})%>"></asp:BoundColumn

    which renders
    <td><%=getSomething( SomeType )%></td

    But I do have a protected member method of the code behind class calle
    getSomething( someType Type ){ return "something";

    Any thoughts

    thanks/


    "Bill Borg" <> wrote in messag
    news:..
    > You can also write a function in your databinding expression for the imag

    cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where GetMyImage i
    an accessible method (e.g. page.getmyimage), and parms is whatever you wan
    to pass to get the image (the value of Type in this case)
    >> ----- C Newby wrote: ----
    >> Right, but i was hoping for something i could determine at run time

    An
    > thoughts on that? Thanks for your reply
    >>>> "Cowboy (Gregory A. Beamer)" <

    wrote i
    > message news:..
    >> If SQL Server, you can do it in your statement
    >>> , 'image'+imageType+'.gif' As ImageNam
    >>> --

    >> Gregory A. Beame
    >> MVP; MCP: +I, SE, SD, DB
    >>

    *********************************************************************
    >> Think Outside the Box

    >

    *********************************************************************
    >> "C Newby" <> wrote in messag
    >> news:..
    >>> I have a data grid control taht contains a data bound coulm

    calle
    >> "Type"
    >>> Depending on the value of "Type" I want to display a correspondin

    imag
    >> i
    >>> the rendered table cell. So, if the value of type is 1, I want t

    sho
    >>> image1.gif. If the value is 2, I want to show image2.gif. Is ther

    a
    > eas
    >>> way to do this
    >>>>> TIA/
    >>>>>>>
    =?Utf-8?B?QmlsbCBCb3Jn?=, Apr 2, 2004
    #7
  8. C Newby

    C Newby Guest

    Thanks again Bill,

    I was able to get it working this time. One thing though,
    "container.dataitem("myField")" didn't work for me, I ended up using:
    "DataBinder.Eval(Container, "DataItem.myField")". Any thoughts on that?

    Hey again, I really apreciate the help//

    "Bill Borg" <> wrote in message
    news:...
    > You need to use a template column, not a bound column, which you can

    create in the property builder in the idea, or straight in the html. Bound
    column is good for showing something that resolves to text (and the format
    string can format it for you), but you need a picture in this case. Try
    something like this, where GetMyPicture is your function that returns the
    filename of the image you want to show. In my case, I'm passing
    container.dataitem("cs_key"), which will resolve to the cs_key field in the
    datasource. In your case, for cs_key you'll use the column name of your
    type field.
    >
    > <asp:TemplateColumn HeaderText="My Picture Column"><itemtemplate><img

    src='<%# GetMyPicture(container.dataitem("cs_key")) %>'/> (don't forget the
    '#')
    > </itemtemplate></asp:TemplateColumn>
    >
    >
    > Regarding the protected function, that should be fine, but if at run-time

    it can't find it you'll get a semi-friendly message, something to the effect
    of "...GetMyPicture is not declared..."
    >
    > hth,
    >
    > Bill
    >
    > ----- C Newby wrote: -----
    >
    > Actually, that's not working for me. When the page renders, the table

    cell
    > just contains the expression. For example, i have something like:
    > <asp:BoundColumn DataField="Something" HeaderText="Something"
    > DataFormatString="<%=getSomething({0})%>"></asp:BoundColumn>
    >
    > which renders:
    > <td><%=getSomething( SomeType )%></td>
    >
    > But I do have a protected member method of the code behind class

    called
    > getSomething( someType Type ){ return "something"; }
    >
    > Any thoughts?
    >
    > thanks//
    >
    >
    > "Bill Borg" <> wrote in message
    > news:...
    > > You can also write a function in your databinding expression for

    the image
    > cell, e.g. imageurl (or src)=<%# GetMyImage(parms) %>, where

    GetMyImage is
    > an accessible method (e.g. page.getmyimage), and parms is whatever

    you want
    > to pass to get the image (the value of Type in this case).
    > >> ----- C Newby wrote: -----
    > >> Right, but i was hoping for something i could determine at

    run time.
    > Any
    > > thoughts on that? Thanks for your reply.
    > >>>> "Cowboy (Gregory A. Beamer)"

    <>
    > wrote in
    > > message news:...
    > >> If SQL Server, you can do it in your statement:
    > >>> , 'image'+imageType+'.gif' As ImageName
    > >>> --
    > >> Gregory A. Beamer
    > >> MVP; MCP: +I, SE, SD, DBA
    > >>>

    >

    **********************************************************************
    > >> Think Outside the Box!
    > >>

    >

    **********************************************************************
    > >> "C Newby" <> wrote in message
    > >> news:...
    > >>> I have a data grid control taht contains a data bound coulmn

    > called
    > >> "Type".
    > >>> Depending on the value of "Type" I want to display a

    corresponding
    > image
    > >> in
    > >>> the rendered table cell. So, if the value of type is 1, I want to

    > show
    > >>> image1.gif. If the value is 2, I want to show image2.gif. Is

    there
    > an
    > > easy
    > >>> way to do this?
    > >>>>> TIA//
    > >>>>>>>
    C Newby, Apr 2, 2004
    #8
    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. Thirsty Traveler

    Can a grid column bound to a DateTime be null?

    Thirsty Traveler, May 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    463
    Thirsty Traveler
    May 17, 2006
  2. Mario Krsnic
    Replies:
    0
    Views:
    377
    Mario Krsnic
    Jun 23, 2006
  3. Chris
    Replies:
    2
    Views:
    679
    Chris
    Mar 4, 2007
  4. TS
    Replies:
    3
    Views:
    405
    Walter Wang [MSFT]
    Aug 16, 2007
  5. Replies:
    10
    Views:
    336
    Sean O'Halpin
    Jul 17, 2006
Loading...

Share This Page