Change row color in Gridview on mouse over

If you have the need to change the row color of a row in a gridview when you mouse over (and mouse out) you can add some code to the RowDataBound event.

Here is a sample:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{
//Only apply to data rows
if (e.Row.RowType == DataControlRowType.DataRow)
{

//Add mouse over event to change row background color
e.Row.Attributes.Add(“onmouseover”, “this.style.backgroundColor=’Yellow'”);

//Add mouse out event to change row background color back to white
e.Row.Attributes.Add(“onmouseout”, “this.style.backgroundColor=White”);

}

}

Here is the blog site where I found this sample.

Advertisements