4
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
<asp:GridView ID="grd_id" runat="server" CellPadding="6" AutoGenerateColumns="False" OnRowDataBound="grd_RowDataBound">
Thanks
Vishal Joshi
Accepted 4
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;
}
}
}
3
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