• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Change color of row based on bound YES/No column.

I have a YES/NO Field bound to a column in a gridview.  If that column is false, I would like to change color of row, otherwise leave as is.
0
Break40
Asked:
Break40
2 Solutions
 
TimCotteeCommented:
Hi Break40,

In your ItemDataBound event:

If e.item.itemtype=ItemType.ListItem or e.item.itemtype= itemtype.alternatingitem then
    if e.item.cells(0).text = "False" Then
        e.item.backcolor=color.red
    End if
End if

Tim Cottee
0
 
Swapnil PipariaArchitectCommented:
Hi Break40,
 you can do it in rowdatabound event by following way

void gvSSLookup_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            string lblYesNovalue = e.Row.Cells[1].Text;
            if (lblYesNovalue == "Yes")
                e.Row.BackColor = "color1"
            else
                e.Row.BackColor = "color2"

          }
    }
Regards,
NetSwap
0
 
Break40Author Commented:
I found another posting and came up with following that works when added to the "gvWorkweek2.RowDataBound" event.  

I also added a line ".Cells(0).Visible = False" that hides first column, which  has the edit and delete tags.  But when I do this the header text for the column is still there.  How can I hide that as well?  Here is my code.


With e.Row
            Dim rv As Data.DataRow
            If .RowType = DataControlRowType.DataRow Then
                rv = CType(.DataItem, Data.DataRowView).Row
                ' Change color
                If Not IsDBNull(rv("Approved")) Then
                    If rv("Approved") = "True" Then
                        .BackColor = Drawing.Color.Red
                        .Cells(0).Visible = False
                    Else

                    End If
                End If
            End If
        End With
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now