problem with e.Row.Cells() of gridview

Discussion in 'ASP .Net' started by Chris, May 8, 2008.

  1. Chris

    Chris Guest

    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, May 8, 2008
    #1
    1. Advertising

  2. Chris

    Chris Guest

    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" <> schreef in bericht
    news:%...
    > 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, May 9, 2008
    #2
    1. Advertising

  3. Chris

    Chris Guest

    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]" <> schreef in bericht
    news:%...
    > "Chris" <> wrote in message
    > news:...
    >
    >> 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, May 9, 2008
    #3
  4. Chris

    Chris Guest

    I found it:

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


    Chris" <> schreef in bericht
    news:%...

    > 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]" <> schreef in bericht
    > news:%...
    >> "Chris" <> wrote in message
    >> news:...
    >>
    >>> 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, May 9, 2008
    #4
    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. Replies:
    0
    Views:
    510
  2. H5N1
    Replies:
    0
    Views:
    2,696
  3. Replies:
    5
    Views:
    645
  4. Joel Finkel

    Cells[].Text or Cells[].Controls[0]

    Joel Finkel, Sep 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    292
    Joel Finkel
    Sep 1, 2003
  5. D
    Replies:
    0
    Views:
    195
Loading...

Share This Page