Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Change GridView Row Cell Color When Text Match From Textbox
WhatsApp
ANIL KUMAR GUPTA
8y
19.2
k
0
0
25
Blog
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
Up Next
How To Apply Cell Text & Background Color In An Excel Sheet Using EPPlus (C#) - Part Two
Ebook Download
View all
Programming Dictionary in C#
Read by 29k people
Download Now!
Learn
View all
Membership not found