Replace a value wiith a graphic in a datagrid?

Discussion in 'ASP .Net' started by simon, Mar 21, 2006.

  1. simon

    simon Guest

    hello,
    i am displaying a dataset in a datagrid, for one of the values being
    displayed it either comes back as a 1 or a 0, which is currently bound
    to a column in the datagrid
    what i'd like to do is that if the value equals 1, replace that value
    with a small icon image. if it equals 0 then have nothing display in
    that column.
    can this be done? please bear in mind that i'm relatively new to
    asp.net. i'm currently using VS2005 and .net 2.0
    thank you for any help

    source....

    in codebehind:
    ResultsGrid.DataBind()

    in aspx page:
    <asp:BoundColumn DataField="PrefPicked" HeaderText="Preference
    Selected">
    <HeaderStyle ForeColor="#000000"></HeaderStyle>
    <ItemStyle CssClass="bodytext"></ItemStyle>
    </asp:BoundColumn>
     
    simon, Mar 21, 2006
    #1
    1. Advertising

  2. Hi Simon,

    For that, I'd use a template column, some inline code and an iif()
    statement. Depending on the value from the field, you reference a different
    image.

    Here's an example that should get you started.

    Let us know if it helps?

    Ken
    Microsoft MVP [ASP.NET]

    <%@ page language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    Function CreateDataSource() As Data.DataTable
    Dim dt As New Data.DataTable
    Dim dr As Data.DataRow
    dt.Columns.Add(New Data.DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New Data.DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New Data.DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New Data.DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 5
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function

    Protected Sub Page_Load _
    (ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If Not IsPostBack Then
    dg.DataSource = CreateDataSource()
    dg.DataBind()
    End If
    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:datagrid id="dg" runat="server"
    autogeneratecolumns="False">
    <columns>
    <asp:templatecolumn headertext="Language">
    <itemtemplate>
    <asp:image id="Image1" runat="server"
    imageurl='<%#"http://www.gc.ca/images/" &
    iif((eval("boolean")=true),"francaisbt.gif","englishbt.gif")%>' />
    </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
    <itemtemplate>
    <asp:label id="lbl" runat="server" text='<%#
    eval("boolean")%>'></asp:label>
    </itemtemplate>
    </asp:templatecolumn>
    </columns>
    </asp:datagrid>
    <asp:label id="Label1" runat="server"
    text="Label"></asp:label></div>
    </form>
    </body>
    </html>

    "simon" <> wrote in message
    news:...
    > hello,
    > i am displaying a dataset in a datagrid, for one of the values being
    > displayed it either comes back as a 1 or a 0, which is currently bound
    > to a column in the datagrid
    > what i'd like to do is that if the value equals 1, replace that value
    > with a small icon image. if it equals 0 then have nothing display in
    > that column.
    > can this be done? please bear in mind that i'm relatively new to
    > asp.net. i'm currently using VS2005 and .net 2.0
    > thank you for any help
    >
    > source....
    >
    > in codebehind:
    > ResultsGrid.DataBind()
    >
    > in aspx page:
    > <asp:BoundColumn DataField="PrefPicked" HeaderText="Preference
    > Selected">
    > <HeaderStyle ForeColor="#000000"></HeaderStyle>
    > <ItemStyle CssClass="bodytext"></ItemStyle>
    > </asp:BoundColumn>
    >
     
    Ken Cox - Microsoft MVP, Mar 21, 2006
    #2
    1. Advertising

  3. simon

    simon Guest

    wow. thank you for that great example.
    basically, all i'd need is this section:

    <asp:templatecolumn headertext="Language">
    <itemtemplate>
    <asp:image id="Image1" runat="server"
    imageurl='<%#"http://www.gc.ca/images/" &
    iif((eval("boolean")=true),"francaisbt.gif","englishbt.gif")%>' />
    </itemtemplate>
    </asp:templatecolumn>

    to evaluate the the value and switch between the image and the null
    image, correct? i'll try that out tonight and let you all know how it
    turns out. thank you very much.


    On Mon, 20 Mar 2006 22:54:24 -0800, "Ken Cox - Microsoft MVP"
    <> wrote:

    >Hi Simon,
    >
    >For that, I'd use a template column, some inline code and an iif()
    >statement. Depending on the value from the field, you reference a different
    >image.
    >
    >Here's an example that should get you started.
    >
    >Let us know if it helps?
    >
    >Ken
    >Microsoft MVP [ASP.NET]
    >
    ><%@ page language="VB" %>
    >
    ><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    ><script runat="server">
    > Function CreateDataSource() As Data.DataTable
    > Dim dt As New Data.DataTable
    > Dim dr As Data.DataRow
    > dt.Columns.Add(New Data.DataColumn _
    > ("IntegerValue", GetType(Int32)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("StringValue", GetType(String)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("CurrencyValue", GetType(Double)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("Boolean", GetType(Boolean)))
    > Dim i As Integer
    > For i = 0 To 5
    > dr = dt.NewRow()
    > dr(0) = i
    > dr(1) = "Item " + i.ToString()
    > dr(2) = 1.23 * (i + 1)
    > dr(3) = (i = 4)
    > dt.Rows.Add(dr)
    > Next i
    > Return dt
    > End Function
    >
    > Protected Sub Page_Load _
    > (ByVal sender As Object, _
    > ByVal e As System.EventArgs)
    > If Not IsPostBack Then
    > dg.DataSource = CreateDataSource()
    > dg.DataBind()
    > End If
    > End Sub
    ></script>
    >
    ><html xmlns="http://www.w3.org/1999/xhtml">
    ><head runat="server">
    > <title>Untitled Page</title>
    ></head>
    ><body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:datagrid id="dg" runat="server"
    >autogeneratecolumns="False">
    > <columns>
    > <asp:templatecolumn headertext="Language">
    > <itemtemplate>
    > <asp:image id="Image1" runat="server"
    >imageurl='<%#"http://www.gc.ca/images/" &
    >iif((eval("boolean")=true),"francaisbt.gif","englishbt.gif")%>' />
    > </itemtemplate>
    > </asp:templatecolumn>
    > <asp:templatecolumn>
    > <itemtemplate>
    > <asp:label id="lbl" runat="server" text='<%#
    >eval("boolean")%>'></asp:label>
    > </itemtemplate>
    > </asp:templatecolumn>
    > </columns>
    > </asp:datagrid>
    > <asp:label id="Label1" runat="server"
    >text="Label"></asp:label></div>
    > </form>
    ></body>
    ></html>
    >
    >"simon" <> wrote in message
    >news:...
    >> hello,
    >> i am displaying a dataset in a datagrid, for one of the values being
    >> displayed it either comes back as a 1 or a 0, which is currently bound
    >> to a column in the datagrid
    >> what i'd like to do is that if the value equals 1, replace that value
    >> with a small icon image. if it equals 0 then have nothing display in
    >> that column.
    >> can this be done? please bear in mind that i'm relatively new to
    >> asp.net. i'm currently using VS2005 and .net 2.0
    >> thank you for any help
    >>
    >> source....
    >>
    >> in codebehind:
    >> ResultsGrid.DataBind()
    >>
    >> in aspx page:
    >> <asp:BoundColumn DataField="PrefPicked" HeaderText="Preference
    >> Selected">
    >> <HeaderStyle ForeColor="#000000"></HeaderStyle>
    >> <ItemStyle CssClass="bodytext"></ItemStyle>
    >> </asp:BoundColumn>
    >>

    >
     
    simon, Mar 21, 2006
    #3
  4. simon

    simon Guest

    that worked perfectly! thank you very much!!

    On Tue, 21 Mar 2006 07:56:24 -0500, simon <> wrote:

    >wow. thank you for that great example.
    >basically, all i'd need is this section:
    >
    ><asp:templatecolumn headertext="Language">
    > <itemtemplate>
    > <asp:image id="Image1" runat="server"
    >imageurl='<%#"http://www.gc.ca/images/" &
    >iif((eval("boolean")=true),"francaisbt.gif","englishbt.gif")%>' />
    > </itemtemplate>
    ></asp:templatecolumn>
    >
    >to evaluate the the value and switch between the image and the null
    >image, correct? i'll try that out tonight and let you all know how it
    >turns out. thank you very much.
    >
    >
    >On Mon, 20 Mar 2006 22:54:24 -0800, "Ken Cox - Microsoft MVP"
    ><> wrote:
    >
    >>Hi Simon,
    >>
    >>For that, I'd use a template column, some inline code and an iif()
    >>statement. Depending on the value from the field, you reference a different
    >>image.
    >>
    >>Here's an example that should get you started.
    >>
    >>Let us know if it helps?
    >>
    >>Ken
    >>Microsoft MVP [ASP.NET]
    >>
    >><%@ page language="VB" %>
    >>
    >><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>
    >><script runat="server">
    >> Function CreateDataSource() As Data.DataTable
    >> Dim dt As New Data.DataTable
    >> Dim dr As Data.DataRow
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("IntegerValue", GetType(Int32)))
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("StringValue", GetType(String)))
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("CurrencyValue", GetType(Double)))
    >> dt.Columns.Add(New Data.DataColumn _
    >> ("Boolean", GetType(Boolean)))
    >> Dim i As Integer
    >> For i = 0 To 5
    >> dr = dt.NewRow()
    >> dr(0) = i
    >> dr(1) = "Item " + i.ToString()
    >> dr(2) = 1.23 * (i + 1)
    >> dr(3) = (i = 4)
    >> dt.Rows.Add(dr)
    >> Next i
    >> Return dt
    >> End Function
    >>
    >> Protected Sub Page_Load _
    >> (ByVal sender As Object, _
    >> ByVal e As System.EventArgs)
    >> If Not IsPostBack Then
    >> dg.DataSource = CreateDataSource()
    >> dg.DataBind()
    >> End If
    >> End Sub
    >></script>
    >>
    >><html xmlns="http://www.w3.org/1999/xhtml">
    >><head runat="server">
    >> <title>Untitled Page</title>
    >></head>
    >><body>
    >> <form id="form1" runat="server">
    >> <div>
    >> <asp:datagrid id="dg" runat="server"
    >>autogeneratecolumns="False">
    >> <columns>
    >> <asp:templatecolumn headertext="Language">
    >> <itemtemplate>
    >> <asp:image id="Image1" runat="server"
    >>imageurl='<%#"http://www.gc.ca/images/" &
    >>iif((eval("boolean")=true),"francaisbt.gif","englishbt.gif")%>' />
    >> </itemtemplate>
    >> </asp:templatecolumn>
    >> <asp:templatecolumn>
    >> <itemtemplate>
    >> <asp:label id="lbl" runat="server" text='<%#
    >>eval("boolean")%>'></asp:label>
    >> </itemtemplate>
    >> </asp:templatecolumn>
    >> </columns>
    >> </asp:datagrid>
    >> <asp:label id="Label1" runat="server"
    >>text="Label"></asp:label></div>
    >> </form>
    >></body>
    >></html>
    >>
    >>"simon" <> wrote in message
    >>news:...
    >>> hello,
    >>> i am displaying a dataset in a datagrid, for one of the values being
    >>> displayed it either comes back as a 1 or a 0, which is currently bound
    >>> to a column in the datagrid
    >>> what i'd like to do is that if the value equals 1, replace that value
    >>> with a small icon image. if it equals 0 then have nothing display in
    >>> that column.
    >>> can this be done? please bear in mind that i'm relatively new to
    >>> asp.net. i'm currently using VS2005 and .net 2.0
    >>> thank you for any help
    >>>
    >>> source....
    >>>
    >>> in codebehind:
    >>> ResultsGrid.DataBind()
    >>>
    >>> in aspx page:
    >>> <asp:BoundColumn DataField="PrefPicked" HeaderText="Preference
    >>> Selected">
    >>> <HeaderStyle ForeColor="#000000"></HeaderStyle>
    >>> <ItemStyle CssClass="bodytext"></ItemStyle>
    >>> </asp:BoundColumn>
    >>>

    >>
     
    simon, Mar 21, 2006
    #4
  5. Thanks for reporting back!

    Feedback helps others who are searching through old answers to know that a
    proposed solution was on the right track.

    Ken

    "simon" <> wrote in message
    news:...
    > that worked perfectly! thank you very much!!
    >
    > On Tue, 21 Mar 2006 07:56:24 -0500, simon <> wrote:
    >
    >>wow. thank you for that great example.
    >>basically, all i'd need is this section:
    >>
    >><asp:templatecolumn headertext="Language">
    >> <itemtemplate>
    >> <asp:image id="Image1" runat="server"
    >>imageurl='<%#"http://www.gc.ca/images/" &
    >>iif((eval("boolean")=true),"francaisbt.gif","englishbt.gif")%>' />
    >> </itemtemplate>
    >></asp:templatecolumn>
    >>
    >>to evaluate the the value and switch between the image and the null
    >>image, correct? i'll try that out tonight and let you all know how it
    >>turns out. thank you very much.
    >>
    >>
    >>On Mon, 20 Mar 2006 22:54:24 -0800, "Ken Cox - Microsoft MVP"
    >><> wrote:
    >>
    >>>Hi Simon,
    >>>
    >>>For that, I'd use a template column, some inline code and an iif()
    >>>statement. Depending on the value from the field, you reference a
    >>>different
    >>>image.
    >>>
    >>>Here's an example that should get you started.
    >>>
    >>>Let us know if it helps?
    >>>
    >>>Ken
    >>>Microsoft MVP [ASP.NET]
    >>>
    >>><%@ page language="VB" %>
    >>>
    >>><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >>>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>>
    >>><script runat="server">
    >>> Function CreateDataSource() As Data.DataTable
    >>> Dim dt As New Data.DataTable
    >>> Dim dr As Data.DataRow
    >>> dt.Columns.Add(New Data.DataColumn _
    >>> ("IntegerValue", GetType(Int32)))
    >>> dt.Columns.Add(New Data.DataColumn _
    >>> ("StringValue", GetType(String)))
    >>> dt.Columns.Add(New Data.DataColumn _
    >>> ("CurrencyValue", GetType(Double)))
    >>> dt.Columns.Add(New Data.DataColumn _
    >>> ("Boolean", GetType(Boolean)))
    >>> Dim i As Integer
    >>> For i = 0 To 5
    >>> dr = dt.NewRow()
    >>> dr(0) = i
    >>> dr(1) = "Item " + i.ToString()
    >>> dr(2) = 1.23 * (i + 1)
    >>> dr(3) = (i = 4)
    >>> dt.Rows.Add(dr)
    >>> Next i
    >>> Return dt
    >>> End Function
    >>>
    >>> Protected Sub Page_Load _
    >>> (ByVal sender As Object, _
    >>> ByVal e As System.EventArgs)
    >>> If Not IsPostBack Then
    >>> dg.DataSource = CreateDataSource()
    >>> dg.DataBind()
    >>> End If
    >>> End Sub
    >>></script>
    >>>
    >>><html xmlns="http://www.w3.org/1999/xhtml">
    >>><head runat="server">
    >>> <title>Untitled Page</title>
    >>></head>
    >>><body>
    >>> <form id="form1" runat="server">
    >>> <div>
    >>> <asp:datagrid id="dg" runat="server"
    >>>autogeneratecolumns="False">
    >>> <columns>
    >>> <asp:templatecolumn headertext="Language">
    >>> <itemtemplate>
    >>> <asp:image id="Image1" runat="server"
    >>>imageurl='<%#"http://www.gc.ca/images/" &
    >>>iif((eval("boolean")=true),"francaisbt.gif","englishbt.gif")%>' />
    >>> </itemtemplate>
    >>> </asp:templatecolumn>
    >>> <asp:templatecolumn>
    >>> <itemtemplate>
    >>> <asp:label id="lbl" runat="server" text='<%#
    >>>eval("boolean")%>'></asp:label>
    >>> </itemtemplate>
    >>> </asp:templatecolumn>
    >>> </columns>
    >>> </asp:datagrid>
    >>> <asp:label id="Label1" runat="server"
    >>>text="Label"></asp:label></div>
    >>> </form>
    >>></body>
    >>></html>
    >>>
    >>>"simon" <> wrote in message
    >>>news:...
    >>>> hello,
    >>>> i am displaying a dataset in a datagrid, for one of the values being
    >>>> displayed it either comes back as a 1 or a 0, which is currently bound
    >>>> to a column in the datagrid
    >>>> what i'd like to do is that if the value equals 1, replace that value
    >>>> with a small icon image. if it equals 0 then have nothing display in
    >>>> that column.
    >>>> can this be done? please bear in mind that i'm relatively new to
    >>>> asp.net. i'm currently using VS2005 and .net 2.0
    >>>> thank you for any help
    >>>>
    >>>> source....
    >>>>
    >>>> in codebehind:
    >>>> ResultsGrid.DataBind()
    >>>>
    >>>> in aspx page:
    >>>> <asp:BoundColumn DataField="PrefPicked" HeaderText="Preference
    >>>> Selected">
    >>>> <HeaderStyle ForeColor="#000000"></HeaderStyle>
    >>>> <ItemStyle CssClass="bodytext"></ItemStyle>
    >>>> </asp:BoundColumn>
    >>>>
    >>>

    >
     
    Ken Cox - Microsoft MVP, Mar 22, 2006
    #5
    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. Brian Blais
    Replies:
    1
    Views:
    382
    Bruno Desthuilliers
    Jun 27, 2006
  2. 50 SOMETHING GAL
    Replies:
    3
    Views:
    401
  3. Andrew Chalk

    Displaying a Graphic in the cell of a datagrid

    Andrew Chalk, Oct 29, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    111
    Eliyahu Goldin
    Oct 30, 2005
  4. simon
    Replies:
    2
    Views:
    195
    simon
    Mar 22, 2006
  5. Andrew Chalk

    Displaying a Graphic in the cell of a datagrid

    Andrew Chalk, Oct 29, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    105
    Eliyahu Goldin
    Oct 30, 2005
Loading...

Share This Page