Gridview -- changing field content

J

Jason Wilson

I would like to change an image in the 1st field in each row of a
gridview based on the content of the other other fields in the row.
Can anyone point me in the right direction?

I tried the following code as a test:

Protected Sub gvRadTurnat_RowCreated(ByVal sender As Object,
ByVal e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then

If CType(DataBinder.Eval(e.Row.DataItem, "193hrs"),
Integer) > 0 Then
e.Row.Cells(0).Style("background-image") =
"redcircle.gif"
End If
End If

End Sub

What I thought this would do is when a row is created if the DataBound
field of "193hrs" had a value greater than 0 it would set the
background image for the cell to "redcircle.gif".
 
M

MikeS

Maybe add an image control to the first column instead of setting the
background-image.


<%@ 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">
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim i As New Image
i.Width = New Unit(60, UnitType.Pixel)
i.Height = New Unit(60, UnitType.Pixel)
If Len(e.Row.Cells(1).Text) < 10 Then
i.ImageUrl =
"http://www.ultraweaver.com/downloads/files/smiley.jpg"
Else
i.ImageUrl =
"http://www.discshoppe.com/Merchant2/graphics/00000001/other/uncle-smiley-240.jpg"
End If
e.Row.Cells(0).Controls.Add(i)
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:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:ImageField>
</asp:ImageField>
</Columns>
</asp:GridView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="System.IO.Directory" SelectMethod="GetFiles">
<SelectParameters>
<asp:parameter Name="path" DefaultValue="c:\" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
 

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
474,085
Messages
2,570,597
Members
47,219
Latest member
Geraldine7

Latest Threads

Top