[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

changing gridview backcolor using VB.net

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.
1 Solution
Robert SchuttSoftware EngineerCommented:
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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