Home All Groups Group Topic Archive Search About

Gridview -- changing field content

Author
16 Nov 2006 9:35 PM
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".

Author
16 Nov 2006 10:44 PM
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>