I want to change a particular row color of a gridview based on some condition.
Loading
I want to change a particular row color of a gridview based on some condition.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Apr 1, 2024, 4:22 AM EDT
Hello
You can use the Grid Row Data Bound event to set conditions based on row color. please check the below sample code.
cs File Code
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (Convert.DateTime(DataBinder.Eval(e.Row.DataItem, "Date")) == DateTime.Now.AddDays(-10)) { e.Row.BackColor = System.Drawing.Color.Cyan; } } }Aspx Page Code
Thanks
Vishal Joshi
Amit MohantyRank #112 · 20K reputationPosted Apr 1, 2024, 3:59 AM EDT
You can change the color of a particular row in a GridView based on some condition by using the RowDataBound event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string value = DataBinder.Eval(e.Row.DataItem, "ColumnName").ToString(); if (value == "SomeCondition") { e.Row.BackColor = System.Drawing.Color.Yellow; } } }Abhishek YadavRank #217 · 11.3K reputationPosted Apr 8, 2024, 5:46 AM EDT
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // You can change this condition as per your requirement string value = DataBinder.Eval(e.Row.DataItem, "ColumnName").ToString(); if (value == "SomeCondition") { e.Row.BackColor = System.Drawing.Color.Yellow; // Change color to yellow // You can change the color to any other color as per your requirement } } }try this