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

x
  • 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.
0
ocdc
Asked:
ocdc
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?
aspx:
<%@ 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GVNutrition" runat="server" OnRowDataBound="GVNutrition_RowDataBound">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

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")})
        Next
        GVNutrition.DataSource = dt
        GVNutrition.DataBind()
    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

output:
capture of output
0

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