TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Change GridView Row Cell Color When Text Match From Textbox
ANIL KUMAR GUPTA
May 12, 2016
18.8
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog you will learn how to 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.
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeBehind=
"Demo.aspx.cs"
Inherits=
"architecture.Demo"
%>
<!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></title>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div> <label>Enter First Name</label>
<asp:TextBox ID=
"txtColor"
runat=
"server"
></asp:TextBox>
<asp:Button ID=
"btnAdd"
runat=
"server"
Text=
"Submit"
OnClick=
"Submit"
/> <br />
<asp:GridView ID=
"gvlist"
runat=
"server"
AutoGenerateColumns=
"false"
OnRowDataBound=
"GridView1_RowDataBound"
CellPadding=
"4"
ForeColor=
"#333333"
>
<Columns>
<asp:BoundField DataField=
"FirstName"
HeaderText=
"FirstName"
/>
<asp:BoundField DataField=
"LastName"
HeaderText=
"FirstName"
/> </Columns>
<HeaderStyle BackColor=
"#5D7B9D"
Font-Bold=
"True"
ForeColor=
"White"
/>
<RowStyle BackColor=
"#F7F6F3"
ForeColor=
"#333333"
/> </asp:GridView>
</div>
</form>
</body>
</html>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Configuration;
using
System.Data.SqlClient;
using
System.IO;
namespace
architecture
{
public
partial
class
Demo: System.Web.UI.Page
{
SqlConnection con;
DataTable dt;
//Connection String from Web.config file
public
static
string
constr = ConfigurationManager.ConnectionStrings[
"ConString"
].ConnectionString;
protected
void
Page_Load(
object
sender, EventArgs e)
{
lblTime.Text = DateTime.Today.DayOfWeek.ToString();
if
(!IsPostBack)
{
ShowData();
}
}
//method for Displaying Data in Gridview
protected
void
ShowData()
{
SqlConnection con =
new
SqlConnection(constr);
string
str =
"Select * from TbDemo; select * from TbDemo"
;
con.Open();
SqlCommand cmd =
new
SqlCommand(str, con);
DataTable dt =
new
DataTable();
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
DataView dv = dt.DefaultView;
gvlist.DataSource = dv;
gvlist.DataBind();
int
i = 0;
foreach
(DataRow dr
in
dt.Rows)
{
if
(i == 3)
{
Response.Write(dr[
"FirstName"
] +
"</br>"
);
}
i++;
}
con.Close();
}
//RowDataBound Event
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
//Checking the RowType of the Row
if
(txtColor.Text.Trim() !=
""
)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
if
(e.Row.Cells[0].Text == txtColor.Text) {
e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;;
}
}
}
}
protected
void
Submit(
object
sender, EventArgs e) {
ShowData();
}
}
}
OutPut
Change GridView Row Cell Color When Text Match From Textbox
Next Recommended Reading
How To Apply Cell Text & Background Color In An Excel Sheet Using EPPlus (C#) - Part Two