In the example I am showing data from the following table:

Data from table
Figure 1

The following is the script of my Employee table:

  1. CREATE TABLE [dbo].[Employee](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Email] [varchar](500) NULL,
  5. [Country] [varchar](50) NULL,
  6. [ProjectID] [int] NULL,
  7. [ManagerName] [varchar](50) NULL,
  8. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  9. (
  10. [ID] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
  12. IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
  13. ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  14. ) ON [PRIMARY]
  15. GO
The following is the data in my Employee table:

Data from table
Figure 2.

I am making a join with the Project table when showing records from this table.

The following is the Project table:

Project Table
Figure 3

The following is the script of my Project table:
  1. CREATE TABLE [dbo].[Project](
  2. [ProjectID] [int] IDENTITY(1,1) NOT NULL,
  3. [ProjectName] [varchar](50) NULL,
  4. [ProjectLeader] [varchar](50) NULL,
  5. CONSTRAINT [PK_Project] PRIMARY KEY CLUSTERED
  6. (
  7. [ProjectID] ASC
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
  9. IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
  10. ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  11. ) ON [PRIMARY]
  12. GO
The following is the data in my Project table:

Data in my Project Table
Figure 4

The following is the aspx code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default3.aspx.cs" Inherits="Contains_In_jQuery.Default3" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>jQuery: Change Row Back Color</title>
  6. <script src="Scripts/jquery-2.1.4.min.js"></script>
  7. <style type="text/css">
  8. body {
  9. font-family: Arial;
  10. font-size: 10pt;
  11. }
  12. td {
  13. cursor: pointer;
  14. }
  15. .hover_row {
  16. background-color: #A1DCF2;
  17. }
  18. </style>
  19. <script type="text/javascript">
  20. $(document).ready(function () {
  21. $('#gvData').empty();
  22. $.ajax({
  23. type: "POST",
  24. contentType: "application/json; charset=utf-8",
  25. url: "Default3.aspx/BindEmployees",
  26. data: "{}",
  27. dataType: "json",
  28. success: function (result) {
  29. $("#gvData").append("<tr style='background-color:red; color:white; font-weight:bold;'><td style='text-align:left;'>ID</td><td style='text-align:left;'>Name</td><td style='text-align:left;'>Email</td><td style='text-align:left;'>Country</td><td style='text-align:left;'>Project name</td><td style='text-align:left;'>Manager Name</td></tr>");
  30. for (var i = 0; i < result.d.length; i++) {
  31. var Country = result.d[i].Country;
  32. if (Country.indexOf('India') != -1) {
  33. $("#gvData").append("<tr style='background-color:#DC143C; font-family:Verdana; font-size:10pt;color:White;'><td style='text-align:left;'>" + result.d[i].ID + "</td><td style='text-align:left;'>" + result.d[i].Name + "</td><td style='text-align:left;'>" + result.d[i].Email + "</td><td style='text-align:left;'>" + result.d[i].Country + "</td><td style='text-align:left;'>" + result.d[i].ProjectID + "</td><td style='text-align:left;'>" + result.d[i].ManagerName + "</td></tr>");
  34. }
  35. else {
  36. $("#gvData").append("<tr style='background-color:skyblue; font-family:Verdana; font-size:10pt ;'><td style='text-align:left;'>" + result.d[i].ID + "</td><td style='text-align:left;'>" + result.d[i].Name + "</td><td style='text-align:left;'>" + result.d[i].Email + "</td><td style='text-align:left;'>" + result.d[i].Country + "</td><td style='text-align:left;'>" + result.d[i].ProjectID + "</td><td style='text-align:left;'>" + result.d[i].ManagerName + "</td></tr>");
  37. }
  38. }
  39. },
  40. error: function (result) {
  41. alert("Error");
  42. }
  43. });
  44. $(function () {
  45. $("[id*=gvData] td").hover(function () {
  46. $("td", $(this).closest("tr")).addClass("hover_row");
  47. }, function () {
  48. $("td", $(this).closest("tr")).removeClass("hover_row");
  49. });
  50. });
  51. });
  52. </script>
  53. </head>
  54. <body>
  55. <form id="form1" runat="server">
  56. <table style="width: 100%; text-align: center; border: solid 5px red; background-color: orange; vertical-align: top;">
  57. <tr>
  58. <td>
  59. <div>
  60. <fieldset style="width: 99%;">
  61. <legend style="font-size: 20pt; color: white; font-family: Verdana">jQuery Set Row Back Color By Checking Column Value</legend>
  62. <table style="width: 100%;">
  63. <tr>
  64. <td style="vertical-align: top; background-color: yellowgreen; text-align: center;">
  65. <asp:GridView ID="gvData" runat="server" CellPadding="4" ShowHeaderWhenEmpty="True"
  66. BackColor="White" GridLines="Both"
  67. BorderColor="#CC9966" BorderStyle="None" Width="90%" BorderWidth="1px" HorizontalAlign="Center">
  68. <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
  69. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
  70. <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
  71. <RowStyle BackColor="White" ForeColor="#330099" />
  72. <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
  73. <SortedAscendingCellStyle BackColor="#FEFCEB" />
  74. <SortedAscendingHeaderStyle BackColor="#AF0101" />
  75. <SortedDescendingCellStyle BackColor="#F6F0C0" />
  76. <SortedDescendingHeaderStyle BackColor="#7E0000" />
  77. </asp:GridView>
  78. </td>
  79. </tr>
  80. </table>
  81. </fieldset>
  82. </div>
  83. </td>
  84. </tr>
  85. </table>
  86. </form>
  87. </body>
  88. </html>
Now my aspx.cs code is:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. namespace Contains_In_jQuery
  11. {
  12. public partial class Default3 : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (!Page.IsPostBack)
  17. {
  18. BindGridWithDummyRow();
  19. }
  20. }
  21. public void BindGridWithDummyRow()
  22. {
  23. DataTable dt = new DataTable();
  24. dt.Columns.Add("ID");
  25. dt.Columns.Add("Name");
  26. dt.Columns.Add("Email");
  27. dt.Columns.Add("Country");
  28. dt.Columns.Add("ProjectID");
  29. dt.Columns.Add("ManagerName");
  30. gvData.DataSource = dt;
  31. gvData.DataBind();
  32. }
  33. [WebMethod]
  34. public static Employee[] BindEmployees()
  35. {
  36. string connectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog= TestDB; Integrated Security=true;";
  37. DataTable dt = new DataTable();
  38. List<Employee> employeeList = new List<Employee>();
  39. using (SqlConnection con = new SqlConnection(connectionString))
  40. {
  41. using (SqlCommand command = new SqlCommand("select e.ID, e.Name,e.Email,e.Country,ProjectName,e.ManagerName from Employee as e Inner join project as p on e.ProjectID=p.ProjectID ORDER BY e.Name", con))
  42. {
  43. con.Open();
  44. SqlDataAdapter da = new SqlDataAdapter(command);
  45. da.Fill(dt);
  46. foreach (DataRow dtrow in dt.Rows)
  47. {
  48. Employee employee = new Employee();
  49. employee.ID = Convert.ToInt32(dtrow["ID"].ToString());
  50. employee.Name = dtrow["Name"].ToString();
  51. employee.Email = dtrow["Email"].ToString();
  52. employee.Country = dtrow["Country"].ToString();
  53. employee.ProjectID = dtrow["ProjectName"].ToString();
  54. employee.ManagerName = dtrow["ManagerName"].ToString();
  55. employeeList.Add(employee);
  56. }
  57. }
  58. }
  59. return employeeList.ToArray();
  60. }
  61. }
  62. }
Here when showing records I am setting the row background color where the Country value is India:

row back color
Figure 5

If you want to set Column background color then use the following changes:
  1. $(document).ready(function() {
  2. $('#gvData').empty();
  3. $.ajax({
  4. type: "POST",
  5. contentType: "application/json; charset=utf-8",
  6. url: "Default3.aspx/BindEmployees",
  7. data: "{}",
  8. dataType: "json",
  9. success: function(result) {
  10. $("#gvData").append("<tr style='background-color:red; color:white; font-weight:bold;'><td style='text-align:left;'>ID</td><td style='text-align:left;'>Name</td><td style='text-align:left;'>Email</td><td style='text-align:left;'>Country</td><td style='text-align:left;'>Project name</td><td style='text-align:left;'>Manager Name</td></tr>");
  11. for (var i = 0; i < result.d.length; i++) {
  12. var Country = result.d[i].Country;
  13. if (Country.indexOf('India') != -1) {
  14. $("#gvData").append("<tr style='background-color:#DC143C; font-family:Verdana; font-size:10pt;color:White;'><td style='text-align:left;'>" + result.d[i].ID + "</td><td style='text-align:left;'>" + result.d[i].Name + "</td><td style='text-align:left;'>" + result.d[i].Email + "</td><td style='text-align:left; background-color:green;'>" + result.d[i].Country + "</td><td style='text-align:left;'>" + result.d[i].ProjectID + "</td><td style='text-align:left;'>" + result.d[i].ManagerName + "</td></tr>");
  15. } else {
  16. $("#gvData").append("<tr style='background-color:skyblue; font-family:Verdana; font-size:10pt ;'><td style='text-align:left;'>" + result.d[i].ID + "</td><td style='text-align:left;'>" + result.d[i].Name + "</td><td style='text-align:left;'>" + result.d[i].Email + "</td><td style='text-align:left;'>" + result.d[i].Country + "</td><td style='text-align:left;'>" + result.d[i].ProjectID + "</td><td style='text-align:left;'>" + result.d[i].ManagerName + "</td></tr>");
  17. }
  18. }
  19. },
  20. error: function(result) {
  21. alert("Error");
  22. }
  23. });
set Column background color
Figure 6