changing gridview backcolor using

Posted on 2014-08-27
Last Modified: 2014-08-29
GVNutrition =  is the Gridview name  that I want to change backcolor  of rows   to red when Risk Assessment column contains the word Positive.  P.S. Risk Assessment column pulls the values from  Test_Result (data source).

my current code is as shown below. But it doest change the backcolor to red. No change happens.

Sub GVNutrition_RowDataBound(sender As Object, e As GridViewRowEventArgs)

      If DataBinder.Eval(e.Row.DataItem, "Test_Result") = "Positive" Then
                  e.Row.BackColor = System.Drawing.Color.Red
            End If
End Sub

Thank you for your time.
Question by:ocdc
    1 Comment
    LVL 35

    Accepted Solution

    Your code for the RowDataBound event works fine for me. The problem must be somewhere else, like the way you fill the datasource or bind the event to the GridView. Can you post more code or have a look at what I did to reconstruct your problem?
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="EE_Q_28506751_WEB._Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <title> EE Q_28506751 </title>
        <form id="form1" runat="server">
            <asp:GridView ID="GVNutrition" runat="server" OnRowDataBound="GVNutrition_RowDataBound">

    Open in new window

    code behind:
    Public Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim dt As New DataTable
            dt.Columns.AddRange({New DataColumn("Id", GetType(Integer)), New DataColumn("Description", GetType(String)), New DataColumn("Test_Result", GetType(String))})
            For i As Integer = 1 To 10
                dt.Rows.Add({i, "Item " & i, IIf(i Mod 3 = 0, "Negative", "Positive")})
            GVNutrition.DataSource = dt
        End Sub
        Sub GVNutrition_RowDataBound(sender As Object, e As GridViewRowEventArgs)
            If DataBinder.Eval(e.Row.DataItem, "Test_Result") = "Positive" Then
                e.Row.BackColor = System.Drawing.Color.Red
            End If
        End Sub
    End Class

    Open in new window

    capture of output

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now