protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_Code = (Label)e.Row.FindControl(“lblCode”);
if (lbl_Code.Text == “1”)
{
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(“#f2d9d9”);
}
}
}
Interviews16 answers
how to change row background color in gridview
16 Answers
Jiyaul MustaphaPosted Jul 10, 2019, 8:41 PM EDT
In data bound event
if (e.Row.RowType = DataControlRowType.DataRow)
{
e.Row.BackColor = System.Drawing.Color.Orange;
}
Vamsi KrishnaPosted Oct 7, 2015, 4:06 AM EDT
You can change row background color in OnRowDataBound event of Gridview.
Steps:
1.create OnRowDataBound event.
protected void grd_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow){
//do something
}
}
2. Find particular row which you want change the background color of Grid.
String dept=e.Row.Cells[1].Text ;
3. Write the condition for checking the value. Example is there is Department column, check the value is IT or not.
If(dept=="IT")
4. If value is OT then assign background color for that row. Ex:- e.Row.BackgroundColor=System.Drawing.Colo}
Narasimha Reddy ChennupalliPosted Jun 25, 2015, 7:50 PM EDT
protected void gvReviewAnswer_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
string User_Answer = (string)DataBinder.Eval(e.Row.DataItem, "User_Answer");
string CorrectAnswer = (string)DataBinder.Eval(e.Row.DataItem, "CorrectAnswer");
if (User_Answer == "0000") {
e.Row.BackColor = System.Drawing.Color.PaleGoldenrod;
} else if (User_Answer == CorrectAnswer) {
e.Row.BackColor = System.Drawing.Color.PaleGreen;
} else if (User_Answer != CorrectAnswer) {
e.Row.BackColor = System.Drawing.Color.Pink;
}
}
Pankaj Kumar ChoudharyPosted May 27, 2015, 8:47 AM EDT
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int quantity = int.Parse(e.Row.Cells[1].Text);
foreach (TableCell cell in e.Row.Cells)
{
if (quantity == 0)
{
cell.BackColor = Color.Red;
}
if (quantity > 0 && quantity <= 50)
{
cell.BackColor = Color.Yellow;
}
if (quantity > 50 && quantity <= 100)
{
cell.BackColor = Color.Orange;
}
}
}
}
Nitin ChoudharyPosted Jan 15, 2015, 9:21 PM EST
if (e.Row.RowType = DataControlRowType.DataRow)
{
//Check your condition here, Cells[1] for ex. is DONE/Not Done column
If(e.Row.Cells[1].Text == "DONE")
{
e.Row.BackColor = Drawing.Color.Green // This will make row back color green
}
}
Nitin ChoudharyPosted Jan 15, 2015, 9:10 PM EST
gridviewid.rows[e.selectindex].backcolor=system.drawing.color.desiredcolor
NareshPosted Jan 12, 2015, 10:31 PM EST
http://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row-Background-Color-based-on-condition-in-ASPNet-using-C-and-VBNet.aspx
Munesh SharmaPosted Jan 7, 2015, 1:56 AM EST
By using we can call RowDataBound Event.or else we can assign directly in aspx page.
Sarath KumarPosted Jan 4, 2015, 7:19 AM EST
refer my article
http://www.c-sharpcorner.com/UploadFile/0c1bb2/highlighting-particular-row-of-gridview-in-specific-conditio/
Vithal WadjePosted Dec 31, 2014, 8:14 AM EST
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Aqua;
varsha dodiyaPosted Dec 29, 2014, 7:24 PM EST
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Aqua;
varsha dodiyaPosted Dec 29, 2014, 7:24 PM EST
Create GridView1_RowDataBound event for your GridView.
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Check your condition here
//Get Id from here and based on Id check value in the
//underlying dataSource Row where you have "DONE" column value
// e.g.
// (gridview.DataSource as DataTable), now you can find your row and cell
// of "Done"
If(Condition True)
{
e.Row.BackColor = Drawing.Color.Red; // your color settings
}
}
Nayeem MansooriPosted Dec 22, 2014, 3:21 AM EST
protected void gvReviewAnswer_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
string User_Answer = (string)DataBinder.Eval(e.Row.DataItem, "User_Answer");
string CorrectAnswer = (string)DataBinder.Eval(e.Row.DataItem, "CorrectAnswer");
if (User_Answer == "0000") {
e.Row.BackColor = System.Drawing.Color.PaleGoldenrod;
} else if (User_Answer == CorrectAnswer) {
e.Row.BackColor = System.Drawing.Color.PaleGreen;
} else if (User_Answer != CorrectAnswer) {
e.Row.BackColor = System.Drawing.Color.Pink;
}
}
Khargesh RajputPosted Dec 21, 2014, 11:47 PM EST
use the css
background-color=#aabbcc;
Pankaj PandeyPosted Dec 18, 2014, 10:41 PM EST
We can change by using javascript function and direct command also
1. document.getElementById(row).style.backgroundColor = "#ffffda";
GridView1_RowDataBound event
e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" & e.Row.ClientID & "')");
2. Create GridView1_RowDataBound event for your GridView.
if (e.Row.RowType == DataControlRowType.DataRow)
{
If(Condition True)
{
e.Row.BackColor = Drawing.Color.Red;
}
}
foreach (TableCell cell in e.Row.Cells)
{
if (D_inshed >= 6)
{
cell.BackColor = System.Drawing.Color.Orange;
}
}
Sneha PandaPosted Dec 15, 2014, 8:07 AM EST











