Replace a value wiith a graphic in a datagrid?

S

simon

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>
 
K

Ken Cox - Microsoft MVP

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>
 
S

simon

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.


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 said:
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>
 
S

simon

that worked perfectly! thank you very much!!

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.


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 said:
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>
 
K

Ken Cox - Microsoft MVP

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 said:
that worked perfectly! thank you very much!!

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.


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>

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>
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top