Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with e.Row.Cells() of gridview

1 view
Skip to first unread message

Chris

unread,
May 8, 2008, 5:21:17 PM5/8/08
to
Hi,

i want to make the forecolor of values into a gridview red but only when
they equals 'n'.

<asp:GridView ID="GridView1" runat="server"
DataKeyNames="hour,place" DataSourceID="SqlDataSource1" >
<Columns>
<asp:CommandField ShowEditButton="True"/>
<asp:BoundField DataField="place" HeaderText="place"
ReadOnly="True" />
<asp:BoundField DataField="hour" HeaderText="hour"
ReadOnly="True" />

<asp:TemplateField HeaderText="theday">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" SelectedValue='<%#
Bind("theday") %>' runat="server">
<asp:ListItem Text="y" Value="y"></asp:ListItem>
<asp:ListItem Text="n" Value="n"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("theday")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>


Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
Dim m As String
m = e.Row.Cells(3).Text
If m = "n" Then
e.Row.Cells(3).ForeColor = Drawing.Color.Red
End If
End If
End Sub

The values are correctly rendered, i can update etc ... but the forecolor
remains black. Is this due to the template?
Thanks for help
Chris


Chris

unread,
May 9, 2008, 2:49:20 AM5/9/08
to
I tested with this line:
response.write(e.Row.Cells(3).Text) which gives nothing ....
but, as i wrote, the gridview renders correctly the values.


"Chris" <s...@qsd.dc> schreef in bericht
news:%23NXS0FV...@TK2MSFTNGP02.phx.gbl...

Mark Rae [MVP]

unread,
May 9, 2008, 6:10:34 AM5/9/08
to
"Chris" <s...@qsd.dc> wrote in message
news:ugEdPDas...@TK2MSFTNGP04.phx.gbl...

> I tested with this line:

> Response.Write(e.Row.Cells(3).Text) which gives nothing ....

Judging by your markup, that would be correct...

> but, as I wrote, the GridView renders correctly the values.

No doubt, but not in the way you think.

Please clarify which of the columns in the GridView (you think) corresponds
to e.Row.Cells(3)...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Chris

unread,
May 9, 2008, 11:49:29 AM5/9/08
to
Thanks for replying.
Well, i think that:
e.Row.Cells(0) is the editbutton
e.Row.Cells(1) is field 'place' (tested with response.write)
e.Row.Cells(2) is field 'hour' (tested with response.write)
so e.Row.Cells(3) is field 'theday'

May i know what you think i think?
Thanks


"Mark Rae [MVP]" <ma...@markNOSPAMrae.net> schreef in bericht
news:%23Gocrzb...@TK2MSFTNGP06.phx.gbl...

Chris

unread,
May 9, 2008, 12:01:50 PM5/9/08
to
I found it:

i need to use e.Row.Cells(3).FindControl("label1")


Chris" <s...@qsd.dc> schreef in bericht

news:%2387nExe...@TK2MSFTNGP02.phx.gbl...

Mark Rae [MVP]

unread,
May 9, 2008, 12:12:43 PM5/9/08
to
"Chris" <s...@qsd.dc> wrote in message
news:%2387nExe...@TK2MSFTNGP02.phx.gbl...

[top-posting corrected]

>> Please clarify which of the columns in the GridView (you think)
>> corresponds to e.Row.Cells(3)...

> so e.Row.Cells(3) is field 'theday'

You mean this: ?


<asp:Label ID="Label2" runat="server" Text='<%# Bind("theday")
%>'></asp:Label>

If so, then e.Row.Cells(3).Text is always going to return an empty string.

Why? Because the field in question doesn't contain any text. It contains an
<asp:Iabel> webcontrol, which isn't the same thing at all as far as ASP.NET
is concerned...

Try this:

Dim MyLabel As Label = e.Row.Cells(3).FindControl("Label2")
Response.Write(MyLabel.Text)

The syntax may not be 100% correct as I never go anywhere near VB.NET...

0 new messages