Change GridView Row Cell Color When Text Match From Textbox

Points
  • Add a aspx page with name demo.
  • Add a Gridview control from toolbox on aspx page in div.
  • Add Text Box and submit button.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="architecture.Demo" %>  
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div> <label>Enter First Name</label>  
  12.                 <asp:TextBox ID="txtColor" runat="server"></asp:TextBox>  
  13.                 <asp:Button ID="btnAdd" runat="server" Text="Submit" OnClick="Submit" /> <br />  
  14.                 <asp:GridView ID="gvlist" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" CellPadding="4" ForeColor="#333333">  
  15.                     <Columns>  
  16.                         <asp:BoundField DataField="FirstName" HeaderText="FirstName" />  
  17.                         <asp:BoundField DataField="LastName" HeaderText="FirstName" /> </Columns>  
  18.                     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  19.                     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> </asp:GridView>  
  20.             </div>  
  21.         </form>  
  22.     </body>  
  23.   
  24.     </html>   
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Configuration;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11. namespace architecture  
  12. {  
  13.     public partial class Demo: System.Web.UI.Page   
  14.     {  
  15.         SqlConnection con;  
  16.         DataTable dt;  
  17.         //Connection String from Web.config file  
  18.         public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;  
  19.         protected void Page_Load(object sender, EventArgs e)  
  20.         {  
  21.                 lblTime.Text = DateTime.Today.DayOfWeek.ToString();  
  22.                 if (!IsPostBack)   
  23.                 {  
  24.                     ShowData();  
  25.                 }  
  26.             }  
  27.             //method for Displaying Data in Gridview  
  28.         protected void ShowData()   
  29.         {  
  30.                 SqlConnection con = new SqlConnection(constr);  
  31.                 string str = "Select * from TbDemo; select * from TbDemo";  
  32.                 con.Open();  
  33.                 SqlCommand cmd = new SqlCommand(str, con);  
  34.                 DataTable dt = new DataTable();  
  35.                 SqlDataReader reader = cmd.ExecuteReader();  
  36.                 dt.Load(reader);  
  37.                 DataView dv = dt.DefaultView;  
  38.                 gvlist.DataSource = dv;  
  39.                 gvlist.DataBind();  
  40.                 int i = 0;  
  41.                 foreach(DataRow dr in dt.Rows)  
  42.                 {  
  43.                     if (i == 3)  
  44.                     {  
  45.                         Response.Write(dr["FirstName"] + "</br>");  
  46.                     }  
  47.                     i++;  
  48.                 }  
  49.                 con.Close();  
  50.             }  
  51.             //RowDataBound Event  
  52.         protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)   
  53.         {  
  54.             //Checking the RowType of the Row  
  55.             if (txtColor.Text.Trim() != "")  
  56.             {  
  57.                 if (e.Row.RowType == DataControlRowType.DataRow)  
  58.                 {  
  59.                     if (e.Row.Cells[0].Text == txtColor.Text) {  
  60.                         e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;;  
  61.                     }  
  62.                 }  
  63.             }  
  64.         }  
  65.         protected void Submit(object sender, EventArgs e) {  
  66.             ShowData();  
  67.         }  
  68.     }  
  69. }  
OutPut