Method – 1 : DataBound Event
This article is used to Change Color of Gridview datarow when data is binding.
When Date in Gridview datarow’s field is today’s date.
Note : lblPostedOn is label which boundfield which is of date type.
protected void gvQuery_DataBound(object sender, EventArgs e)
{
Label sDate = null;
foreach (GridViewRow r in gvQuery.Rows)
{
sDate = (Label)r.FindControl("lblPostedOn");
System.DateTime dt = Convert.ToDateTime(sDate.Text);
if (dt.Date == System.DateTime.Today.Date)
r.BackColor = System.Drawing.Color.Gray;
}
}
Method – 2 : RowCreated Event
Here’s the code for that :
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onMouseOver", "this.style.background='#eeff00'");
e.Row.Attributes.Add("onMouseOut", "this.style.background='#ffffff'");
}